Character Animation Issue

Hello,

In the Basic Movement Section and Lecture number 10, My character does not get back to the Idle state after I left clicking a mouse button. When I left mouse click it was stuck in a running state not moving forward but running animation still goes on and not get back to the idle state.

Post a copy of your Mover script and we’ll take a look at it.

Sure Brian,

void Update()
{
if (Input.GetMouseButton(0))
{
moveToCursor();
UpdateAnimator();
}

}

private void moveToCursor()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool hasHit = Physics.Raycast(ray, out hit);
if (hasHit)
{
GetComponent().destination = hit.point;
}
}

private void UpdateAnimator()
{
    Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
    Vector3 localVelocity = transform.InverseTransformDirection(velocity);
    float speed = localVelocity.z;
    GetComponent<Animator>().SetFloat("ForwardSpeed", speed);
}

Ok, the UpdateAnimator(); code looks correct (aside from the GetComponent().destination, which is caused by GetComponent<NavMeshAgent>() not being in a code block.
Next up is the Animator itself. Take a look at the values in your blend tree.
It helps to move the Animator window alongside the game window while playing and expanding locomotion so that you can see the blend tree in action. Then add this to your UpdateAnimator at the end of the method to see the current forward speed:
Debug.Log($"Forward Speed = {speed)");

You can see the issue here

When I leave click animation remains in a running state and when I click somewhere else it behaves wierdly.

I see what’s happening, but I’m unsure why… It looks like the speed is being set to velocity.z, not localVelocity.z, and looking at the code, this is simply impossible.

Let’s change that Debug.Log statement, then when moving around take a look in the logs, the numbers should be different on each line
Debug.Log($"Velocity = {velocity}, localVelocity = {localVelocity}");

Yes, The values are fluctuating. Not each line but after every 3,4 lines different values

Unless you’re going due north or due south, the values should actually never match.
I’m not sure what’s going on with this, I’d like to take a closer look at your project to see if I can spot the issue. Go ahead and zip up your project (remove the Library folder from the zip to save space) and upload your project to https://gdev.tv/projectupload/ and I’ll take a look at it.

Sir here is the github link of my project

I hope it would be more feasible for you, I can upload on your given link if you still want it.

I was looking in the wrong place, the actual issue was much simpler…

void Update()
{
     if (Input.GetMouseButton(0))
     {
         moveToCursor();
         UpdateAnimator();
      }
}

The UpdateAnimator was only being called when the mouse button was clicked.
Move the UpdateAnimator out of the if block and it will be called every frame, which is what we’re after.

Thank You, Mr brian what a horrible blunder by me. Thanks for your effort :slight_smile:

When it comes to evaluating blunders on a scale of oh, that happens all the time to what a horrible blunder, this one is significantly closer to oh, that happens sometimes. If we wrote all our code perfectly the first time, every time, we would never have the opportunity to learn.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.