Hi, thanks for stopping by, i set up a AI, whose function is to follow around the player. It’s working, the only problem is the animations. It’s bugged, I tried it all, but the AI only does 1 animation. All the code is down below.
Ai controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rita_AI : MonoBehaviour
{
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));
}
}
This is how i set up the Animator: Imgur: The magic of the Internet
The transition idle → blend tree has nothing
The transition blend tree → idle has a condition (isRunning = false)