Unity. Cant play animation in animator - unity3d

i'm making a game in 2d and whenever i create an animation sprite, an animator controller is created automatically, i dont know why, i once tried it but i thought it wouldnt be necesary for animations i just want to make in 2d.. so after trying many times to play an animation without the animator (because unity says i have to set the animation legacy, which i dont know where), i gave it a try to play animations in the animator.. i searched through the script reference and wrote the code just like this:
#pragma strict
var velocity : float = 8;
function Update ()
{
var movement = Input.GetAxis("Horizontal") * velocity;
transform.Translate(Vector2.right * movement * Time.deltaTime);
if(Input.anyKey)
Animator.Play("move");
}
the error that unity says is : Assets/PlayerControl.js(12,26): BCE0020: An instance of type 'UnityEngine.Animator' is required to access non static member 'Play'.
so i tried to do this:
function Start ()
{
//var anim = GetComponent("Animator");
}
function Update ()
{
var movement = Input.GetAxis("Horizontal") * velocity;
transform.Translate(Vector2.right * movement * Time.deltaTime);
if(Input.anyKey)
anim.Play("move");
}
and another error occurs: Assets/PlayerControl.js(17,17): BCE0005: Unknown identifier: 'anim'.
i just want to play one simple animation that will just change the sprites. i know how to play animations without the animator.. so please tell me what to do with it.. how to stop unity auto creating animator controller or set the animation legacy, or how to fix this problem with the animator script.
Update:
i removed the code code just to make other stuff while i search solution for this but now it seems that animator is running this animation no matter what.. i made another state on it as idle, but then it goes to the animation i created.. i really dont understand this.

You can check Unity3D Animation mechanism with example http://docs.unity3d.com/Manual/Animator.html
Unity doesn't give us static method for animation play. See above.
http://docs.unity3d.com/ScriptReference/Animator.html
Try to play of animation
GetComponent("Animator").Play("move", -1, 0.0f);
Check the script reference of official site.
http://docs.unity3d.com/ScriptReference/Animator.Play.html

Related

Unity Hand tracking issue on the quest with UI raycasting

I'm working on a Unity project, trying to test the UI interaction on the Quest II with hand tracking and ray casting.
I've set up a very basic scene with all the default assets (OVRCameraRig, UIHelper) and added just a button to test the UI interaction.
This is what my scene looks like:
The issue I have is, when I run the scene, the Ray is rotated 90 degrees, and it's attached to the wrist for some reason. I made a video here to show what I mean :
https://www.youtube.com/watch?v=5f12yfpugB8
It's still interacting with the UI though.
Then after watching some online tutorials, I commented out these lines in the HandInputSelector.cs, which was attached to the UIHelper:
void SetActiveController(OVRInput.Controller c)
{
/*
Transform t;
if(c == OVRInput.Controller.LTouch)
{
t = m_CameraRig.leftHandAnchor;
}
else
{
t = m_CameraRig.rightHandAnchor;
}
m_InputModule.rayTransform = t;
*/
}
and instead added a 2nd script to the UI helper, with these lines only:
public OVRHand hand;
public OVRInputModule inputModule;
private void Start()
{
inputModule.rayTransform = hand.PointerPose;
}
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
https://youtu.be/q3d0eG2LwY0
My Unity version is 2021.3.1f1
Can someone please tell me what I'm doing wrong?

UNITY 2D - Can't change Child's position

I have created a project and put a character in it, a Ninja. I drew every part of his body and put them together into one organized gameObject. It is organized as this.
Ninja Organization
NOTE : The Left_Arm GameObject has a parent so it can be rotated by the shoulder like a real arm
Now here comes the problem. I've searched and searched for hours and haven't found a solution to something that is surely so simple and dumb. In a script, I try to move the Left_Arm parent for a little animation. Here's the script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NinjaShooting : MonoBehaviour {
[Header("GameObjects")]
public Transform leftArm;
public Transform rightArm;
[Header("Other")]
public bool shooting = false;
// Update is called once per frame
void Update () {
Debug.Log(leftArm.localPosition);
leftArm.localPosition = new Vector3(100, 100, 100);
Debug.Log(leftArm.localPosition);
shooting = false;
}
}
The public variables are all assigned and everything, there are no errors in the console, but the line leftArm.localPosition = new Vector3(100, 100, 100); doesn't seem to do a thing to my character.
Here's is the Console after running it : Image
The console seems alright. The start position is (-0.3, 0.2, 0) and after leftArm.localPosition = new Vector3(100, 100, 100); the position is debugged as (100, 100, 100) as it should be. But the position changes in the inspector, nor in the game window...
Additionnally, the starter position is debugged every frame, but it shouldn't. So I assume that my position isn't taken into account or something.
I've done this so many times, and always has worked.
I even tried with a new project and recreated it, and there, mysteriously, it worked. So there is someting wrong with my organization or something. Sorry if this is a stupid question.
Thank you in advance
PS : Here is an image of the script in the editor, just in case there something wrong with that : Image
PS : The script is a it weird because I simplified it :). But this doesn't work too
This issue is likely caused by an Animator controller "locking" properties that are not actually part of its Motion. In this case, it is "locking" the Transform's position. Unity does not report any warnings or errors when you try to modify those properties, even if they are being controlled by an animation.
The typical offender is the Write Defaults property being set to true for your Animation States in your animation controller. Have a look here at the documentation for what Write Defaults does, and it will make more sense to you. I suggest changing it to false for all animation states that do not need it.

How to stop NavMeshAgent separating from Animator?

I am working on a research project that uses a NavMeshAgent. I currently have a very simple scene where an agent is spawn at the start, walks through an "entrance" trigger collider, an "exit" trigger collider, then ultimately collides with a "destroyer" trigger collider with a script that ends the scene. Nothing complex, no physics collisions should be occurring.
I've been running some simulations both in the editor and in -batchmode -nographics via an executable that logs a basic runtime statistic when the scene ends. I found that in both the Unity editor and the CLI execution that occasionally the scene's execution time would spike. I finally caught what was happening in action- the NavMeshAgent component was becoming detached from my agent and floating out in front of it.
In this picture you can see the two colliders on the agent (one very small through his body for physics and one larger one for his "personal space",) the exit trigger collider (the giant red box on the right,) and floating between the two is a capsule-shaped NavMeshAgent component.
I used this unity page detailing how to use NavMeshAgents with animators, but after recreating their recommended setup, I am still having the issue.
Does anyone have any solutions for anchoring the NavMeshAgent to the agent itself?
I met exactly the same problem, where making the NavMeshAgent component a child and setting the NavMeshAgent's local position in every frame solved the problem.
private NavMeshAgent agent;
void Awake()
{
agent = gameObject.GetComponentInChildren<NavMeshAgent>();
anim = gameObject.GetComponent<Animator> ();
}
private void Update()
{
agent.transform.localPosition = Vector3.zero;
// todo:
// set animator
}
void OnAnimatorMove ()
{
// Update position to agent position
transform.position = agent.nextPosition;
}
For those of you who stumbled here, but for whom the accepted answer did not work. Make sure that navMeshAgent.updatePosition is set to true or that it is not changed within a script. This could be the cause of the separation.
NavMeshAgent navMeshAgent;
// This may cause the agent to separate
navMeshAgent.updatePosition = false;
// This will make sure it is synced
navMeshAgent.updatePosition = true;
This works as of version 2022.1 of the Unity API. Here are the docs for the function : https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-updatePosition.html

Unity type racer

I'm designing a typeracer game in unity where the player is in an athletics 100m sprint and the faster they type, the faster the character runs.
I'm just wondering should I be aiming to get an animation that completes between every correct letter entered or working out an algorithm that speeds up, slows down and pauses the animation depending on whether the letter is correct.
Has anyone had any experience with something like this before?
Also, being quite new to unity i'm just using the standard assets with Ethan as my model, is this the right thing to be doing here?
Original Thoughts
You could have it so that every correct character type speeds up the animation of the character and slowly ticks down per millisecond that passes (i.e slowing down if you aren't typing). Then when the user enters a wrong character the animation gets increasingly slower (1/10 of the previous time every time (?)).
Solution
In Unity, working with timing is a little difficult, my class and I had issues with it this year. The best solution we found is working within the FixedUpdate loop itself, as this is run on a more concise time frame than just Update.
Example
For my solution (and what we all ended up doing) was to update the time in FixedUpdate and use it in Update
void FixedUpdate() {
if (timer >= 12f) stopped = true;
if (!stopped) timerDT = updateTimer(Time.deltaTime);
}
If the timer variable is greater than 12 (seconds) then stop movement.
If not stopped, continue updating and adding to the timer, as well as giving the frame time back to timerDT
void Update() {
this.transform.translate(velocity * timerDT);
}
This will run and update the game object attached based on its velocity and the time frame given in FixedUpdate
For you, I would have the script save the animation controller as a variable in the script:
Controller animation = {animation controller};
Note: Don't remember what needs to go here, but I'm pretty sure it's the controller
Then you can change the animation like so:
void Update() {
update_animation(timerDT, anim_speed);
}
void FixedUpdate() {
timerDT = updateTimer(Time.deltaTime);
if (timerDT - oldDT > 0.1) {
oldDT = timerDT;
anim_speed = anim_speed / 0.1; // for decreasing speed
}
}
void update_animation(float deltatime, float speed) {
animation["run"].speed = anim_speed;
}

How do I use SteamVR_LoadLevel to fade between scenes in unity3d?

I've got this at the top,
public GameObject mainCamera
and then I've got this within an if statement,
SteamVR_LoadLevel tempload = mainCamera.GetComponent<SteamVR_LoadLevel>();
tempload.fadeOutTime = 1f;
tempload.fadeInTime = 1f;
tempload.Trigger();
but I'm getting this error when triggering the if statement,
Coroutine 'LoadLevel' couldn't be started because the the game object 'Camera (head)' is inactive!
Edit 1. I figured it out, I should have made the mainCamera variable of type SteamVR_Camera and I should have nullchecked it as well as tested for it being active and enabled.
Check state game object "Camera (head)" in hierarchy window and active him