- Home /
Idle animation when walking into object
So I want to stop player walking animation when he walks into object, but i don't know how to do it. This is what i have tried:
isMoving = false;
if (Input.GetAxisRaw ("Horizontal") != 0) {
rigid.velocity = new Vector2 (moveSpeed * Input.GetAxisRaw ("Horizontal"), rigid.velocity.y);
lastMove = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0.0f);
} else {
rigid.velocity = new Vector2(0.0f, rigid.velocity.y);
}
if (Input.GetAxisRaw ("Vertical") != 0) {
rigid.velocity = new Vector2 (rigid.velocity.x, moveSpeed * Input.GetAxisRaw ("Vertical"));
lastMove = new Vector2 (0.0f, Input.GetAxisRaw ("Vertical"));
} else {
rigid.velocity = new Vector2(rigid.velocity.x, 0.0f);
}
if (rigid.velocity.x != 0.0f || rigid.velocity.y != 0.0f) {
isMoving = true;
if (rigid.IsTouchingLayers()) {
isMoving = false;
}
}
If isMoving is true, then player activates walking animation, when it's false player activates idle animation.
Problem with this code is, if player walks to the right side of the tree, it activates idle animation as it should, but if i go down or up after hiting the right side of the tree, player is walking with idle animation.
I tried checking if previous frame transform position is same as current frame transform position, but when player walks, it's glitching out, probably because transform position updates not on every frame ?
So i don't know how to do it. Please help me.
Your answer
Follow this Question
Related Questions
Animations don't Update 1 Answer
Can the animation editor create local rotational data? 3 Answers
Unity Timeline, Move characters transform position 1 Answer
Adding animation clips via script 2 Answers
Sprites behaviours is awkward 0 Answers