Cannot get AI to move when using Animator Please help!

So I hope someone can just tell me I’m being stupid and open my eyes to what my issue is.

I am trying to make my enemy move towards the player when the player enters its capsule collier.

Currently all that happens is the animation triggers but the enemy does not move, I am currently working with three states, idle, walking and running.

I have setup Boolean variables that connect to and from the animation states in the animator with true or false values, when I play the game the collier triggers work, when Ienter the trigger the enemy starts running and when i leave it goes back to walking but they do not move.

I have tried setting up Animation events to set the speed or call the move function in the script but nothing seems to work.

If I turn off the animator the enemy will move without animations. I have tried lots of different ways to do this, below is my current code its its not in the best state as I have been changing things all day, I feel as though I am not understanding something fundamental to do with animating AI characters. Any help would be greatly appreciated even if its just pointing me in the right direction to find a solution.

Here is a video to the game object i am talking about.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMovement : MonoBehaviour {

[SerializeField] float moveSpeed;
[SerializeField] GameObject player;
Rigidbody2D enemyRigidbody;
CapsuleCollider2D enemyCapsuleCollider2D;
bool playerInRange;
Animator enemyAnimator;
public Transform target;

// Start is called before the first frame update
void Start() {
enemyRigidbody = GetComponent();
enemyCapsuleCollider2D = GetComponent();
enemyAnimator = GetComponent();
target = player.gameObject.GetComponent();

}

// Update is called once per frame
void Update() {

//need to figure out how to use the localscale to flip the
//sprite and make the enemy change direction based on what side the player hits it
LayerMask playerLayer = LayerMask.GetMask(“Player”);
if (playerInRange && enemyCapsuleCollider2D.IsTouchingLayers(playerLayer)) {
enemyAnimator.SetBool(“isWalking”, false);
enemyAnimator.SetBool(“isRunning”, true);
if(IsFacingRight()) {
transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
}

} else if (!playerInRange && !enemyCapsuleCollider2D.IsTouchingLayers(playerLayer)) {
enemyAnimator.SetBool(“isRunning”, false);
enemyAnimator.SetBool(“isWalking”, true);
MoveEnemy();

}
}

public void SetMovementSpeed(float speed) {
moveSpeed = speed;
}

private void MoveEnemy() {
if(IsFacingRight()) {
enemyRigidbody.velocity = new Vector2(moveSpeed, 0f);
}else {
enemyRigidbody.velocity = new Vector2(-moveSpeed, 0f);
}
}

private bool IsFacingRight() {
return transform.localScale.x > 0;
}

void OnTriggerEnter2D(Collider2D other) {
// If the entering collider is the player…

if (other.gameObject == player) {
// … the player is in range.
playerInRange = true;
//transform.localScale = new Vector2((Mathf.Sign(enemyRigidbody.velocity.x)), 1f);

}
}

void OnTriggerExit2D(Collider2D other) {
// If the exiting collider is the player…
if (other.gameObject == player) {
// … the player is no longer in range.
playerInRange = false;
//transform.localScale = new Vector2(-(Mathf.Sign(enemyRigidbody.velocity.x)), 1f);

}
}[/code]

Do you change the position in your animations? Doing this will probably lock out any movement you do in your scripts. You might want to try to use “Apply Root Motion” on your Animator, that might work.

Ok, PGJ you are my new best friend!

Turning on the apply root motion thing worked!

Thank you so much, I knew it would be something simple but after 2 days of trying and trying i thought id ask

Cheers mate you are the best!

1 Like