- Home /
EnemyAi animations, are bugged while moving
Hi, thanks for stopping by, i made a little script, and an animator in order for an AI to chase a player. because its a topdown i have 4 different directions. When i run the game, the AI only plays 1 animation.
AIScript:
{
public float speed;
public float checkRadius;
public bool shouldrotate;
public LayerMask WhatIsPlayer;
private Transform target;
private Rigidbody2D rb;
private Animator animator;
private Vector2 movement;
public Vector3 dir;
private bool isInChaseRange;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
target = GameObject.FindWithTag("Player").transform;
}
private void Update()
{
animator.SetBool("isRunning", isInChaseRange);
isInChaseRange = Physics2D.OverlapCircle(transform.position, checkRadius, WhatIsPlayer);
dir = target.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
dir.Normalize();
movement = dir;
if (shouldrotate)
{
animator.SetFloat("X", dir.x);
animator.SetFloat("Y", dir.y);
}
}
private void FixedUpdate()
{
if (isInChaseRange)
{
MoveCharacter(movement);
}
}
private void MoveCharacter(Vector2 dir)
{
rb.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime));
}
}
The transition idle -> moving has nothing
The transition moving -> idle has a condition isRunning false
Comment
Your answer
Follow this Question
Related Questions
Collision on specific Frames 1 Answer
Multiple Cars not working 1 Answer
How can I code an enemy attack? (Top Down Melee Attack) 1 Answer
Recoloring animated sprites [Solved] 2 Answers
Jump & Animation Script 2D 0 Answers