- Home /
Enemy shouldn't walk with the player
I am a little bit new at unity, and now I was trying to make enemies. I started like the way how I started with making a player until I wanted to set the walking animation. The walking animation should start, when the speed float is greater then 0.1, I did the same for the player. Now, and for some reason, the walking animation of the enemy starts, when the player moves, even though the enemy is not moving.
How can I fix that?
here is the code of the enemy:
public class Enemy : MonoBehaviour {
public Animator animator;
Rigidbody2D rb2d;
public float runSpeed = 40f;
float horizontalMove = 0f;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("speed", Mathf.Abs(horizontalMove));
}
}
Thanks.
Answer by Optrix · Jul 01, 2019 at 01:33 AM
Input.GetAxisRaw("Horizontal") - this means that you are controlling the enemy with your input controls.
I'd suggest you put something else in here instead of basing enemy movement off your joystick/keyboard input.
horizontalMove = runSpeed * Time.deltaTime
could work.
Answer by $$anonymous$$ · Jul 01, 2019 at 01:41 PM
by using unique animators for each game object that has animation
that wasn't the problem by me, I already did that. But yes, it is also because of that. thx