- Home /
Can't set animator bool to True
I am trying to write a simple point and click movement system. I press a point and my character moves there using navmesh. Everything works. I press a point, a marker is set and my character moves there. There is a small problem with the animation however. First here is the code i am using:
void Update () {
if (Input.GetKeyUp(KeyCode.Mouse0)) {
ray = characterCamera.ScreenPointToRay (Input.mousePosition);
didHit=Physics.Raycast (ray, out hit);
if (didHit && hit.collider.tag=="Travel") {
anim.SetBool ("Walking",true);
Debug.Log ("Walking: true");
if (currentMarker!=null){
Destroy (currentMarker);
}
rayHitPosition=hit.point;
currentMarker=Instantiate (Marker,rayHitPosition,Quaternion.Euler (Vector3.zero));
agent.destination=rayHitPosition;
}
}
if (agent.remainingDistance<=0)
{
anim.SetBool ("Walking",false);
}
}
My Walking bool sometimes won't set to true. When i set press my mouse for the first time the bool won't set to true however if i press again WHILE the agent is in motion then the bool will set to true and the animation play. Anything i am missin? Why won't it set the bool on the first click?
Does RayCast check all collisions within the same frame it is called? I haven't explored raycasts yet, but if it's part of the Physics, then perhaps it only updates on every physics frame, which is not every frame?
To see what's going on, put
Debug.Log(didHit);
Debug.Log(hit.collider.tag);
right under didHit=Physics.Raycast (ray, out hit);
, run the game, and see the console.
I did, It seems they all update at the same time. Also if the hit.collider.tag wasn't Travel and my didHit not true then my character wouldn't be moving at all.
Answer by Yarden-Raveh · Dec 19, 2013 at 08:11 PM
Found the answer. The navmeshagent remainingDistance is only calculated after the frame in which the destination was changed. Instead of checking if agent.remainingDistance is 0 I used vector3.distance to calculate the remaining distance manually.
Your answer
Follow this Question
Related Questions
Is there a way to change the WrapMode of all Animations in a Mecanim Animator? 1 Answer
Mecanim Transition-Arrow Pulsing 2 Answers
unity freezes when running sprinting animation 0 Answers
Animation Mecanim - Parts of model displaces after animating 0 Answers
2D: initial sprite idle animation playing too fast upon game initiation until I input movement keys? 1 Answer