- Home /
Idle Animation Not Playing
Hi all, I am currently testing 2D game development. After finally getting my follower script to work and have a sprite follow me, I am attempting to animate this follower too. I'm sure you can already tell I'm new to Unity and games development in general so please point out anything wrong!
My problem is that my animations are playing for my actual movements (walking up, down, left and right), but whenever I stop my idle animations are not playing even though my isMoving boolean is false, and the moving animations continue to play even when the follower is not moving. Here is my code:
public Transform target;
public float speed;
public float stopDistance = 1f;
Animator anim;
Rigidbody2D rbody;
void Start()
{
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody2D>();
anim.speed = 0.65f;
}
void Update()
{
//follow player
var distance = Vector2.Distance(transform.position, target.position);
float step = speed * Time.deltaTime;
if (distance > stopDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, step);
}
//getting direction of movement and therefore animating it
var heading = target.position - transform.position;
var moveDistance = heading.magnitude;
Vector2 moveDirection;
// Get the direction of the player
moveDirection = heading / moveDistance;
// Make this vector planar (xz-plane)
if (moveDirection != Vector2.zero)
{
anim.SetBool("isMoving", true);
anim.SetFloat("moving_x", moveDirection.x);
anim.SetFloat("moving_y", moveDirection.y);
}
else
{
anim.SetBool("isMoving", false);
}
}
From testing around with the code I think the problem is to do with the var heading = target.position - transform.position;
, because with my stopping distance preventing the follower getting too close, it will always leave a gap distance between the follower and player, and I feel that this gap is always setting the value of 'heading' to be > 0.
I think this is the problem because when I remove the stopping distance all together and disable the sprite renderer, I can see that the follower does not move (/is idle when it is directly on top of the player). As well as the 'moving_x' and 'moving_y' parameters being always above/below 0 and never actually 0 in the animation pane while moving. As stated before I feel like this is caused by the gap inbetween the follower and player caused by the stopping distance, however I cannot work out any fix for this. So any solution would be greatly appreciated!
If i understood correctly the issue, why don't you just check if the heading is less than say 0.1, set heading to 0?
Thanks for your reply. That's a good idea but when I tried:
if (heading < 0.1)
{
heading = 0;
}
It gave me the error 'Operator '>' cannot be applied to operands of type 'Vector2' and 'double''. Have I missed a step here?
Well the answer to this question is that heading is a Vector2. That means there are two values in it, x and y.
You either want to check the direction that you move (probably x) or else the magnitude of x and y if you care about both directions.
if ($$anonymous$$athf.abs(heading.x) < 0.1)
heading = Vector2.zero;
// or
if (heading.magnitude < 0.1)
heading = Vector2.zero;
Answer by Brian-FJ · Mar 28, 2017 at 04:30 PM
Hi Peakz. I think the main thing you're doing wrong is checking if moveDirection is zero. As you said that distance won't ever really be zero.
However you already know everything you need. The line near the start that goes "if (distance > stopDistance)" looks like it does the check you need. What about some code like this?
if (distance > stopDistance)
{
anim.SetBool("isMoving", true);
anim.SetFloat("moving_x", moveDirection.x);
anim.SetFloat("moving_y", moveDirection.y);
}
else
{
anim.SetBool("isMoving", false);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
2d Animation sprites - right way? 0 Answers
Sprite-based animation not loading with Animation component 0 Answers
Need help on skeletal Animation 0 Answers
Need script to do something when animation finished ? 1 Answer