- Home /
Move animation in coroutine problem
Hi,
I'm trying to take my first steps in Unity and I've stumbled upon this very weird issue. I've a pathfinding script, after finding one I'm trying to move player and change his animation to idle/walking with current direction. But when I'm using code below the animation isn't changing at all or is changing with random delays. The result is that player is idle-sliding or sliding with good animation but wrong direction. I've tried changing animation parameters in coroutines but with no success. Big part of the code is from Roguelike tutorial.
public float moveTime = 0.1f;
protected int direction = 2;
protected bool isWalking = false;
void Update() {
anim.SetBool ("walking", isWalking);
anim.SetInteger ("direction", direction);
}
//Protected, virtual functions can be overridden by inheriting classes.
protected virtual void Start ()
{
//Get a component reference to this object's BoxCollider2D
boxCollider = GetComponent <BoxCollider2D> ();
//Get a component reference to this object's Rigidbody2D
rb2D = GetComponent <Rigidbody2D> ();
anim = GetComponent<Animator> ();
//By storing the reciprocal of the move time we can use it by multiplying instead of dividing, this is more efficient.
inverseMoveTime = 1f / moveTime;
}
protected IEnumerator moveAlongPath(List<IntVector2> path) {
isWalking = true;
Debug.Log ("Anim-walking: " + anim.GetBool ("walking"));
foreach (IntVector2 p in path) {
Vector3 to = new Vector3(p.X, p.Y, 0);
changeDirection(to);
Debug.Log ("Anim-direction: " + anim.GetInteger ("direction"));
yield return StartCoroutine(SmoothMovement(to));
}
isWalking = false;
Debug.Log ("Anim-walking: " + anim.GetBool ("walking"));
}
//Co-routine for moving units from one space to next, takes a parameter end to specify where to move to.
protected IEnumerator SmoothMovement (Vector3 end)
{
//Calculate the remaining distance to move based on the square magnitude of the difference between current position and end parameter.
//Square magnitude is used instead of magnitude because it's computationally cheaper.
float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
//While that distance is greater than a very small amount (Epsilon, almost zero):
while(sqrRemainingDistance > float.Epsilon)
{
//Find a new position proportionally closer to the end, based on the moveTime
Vector3 newPostion = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime);
//Call MovePosition on attached Rigidbody2D and move it to the calculated position.
rb2D.MovePosition (newPostion);
//Recalculate the remaining distance after moving.
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
//Return and loop until sqrRemainingDistance is close enough to zero to end the function
yield return null;
}
}
Exit times always 0, the same with transition duration
PS. How can I change speed of movement, because moveTime variable isn't giving any result :(
Your answer
Follow this Question
Related Questions
Passing reference of transform properties to coroutine animation? 1 Answer
Lerp in Nested Coroutines doesn't work if I add some functions inside the Coroutines 1 Answer
How to call a coroutine before an animation? (JS or C#) 2 Answers
My Coroutine does not get called 4 Answers
Animation Curve or Coroutine? Performance question. 1 Answer