Animations not working properly using transform.position

Hi all, I have a problem that I was using a basic enemy follow script for my enemies, but using this has just shown me that animations for my enemies mimic the movements of the player. Basically, If the player walks up, all the enemies show up animation, even through they are moving towards the player!

basically, here is the script I use for the anim controller for the player:

using UnityEngine;
using System.Collections;

public class ScriptPlayerAnimationController : MonoBehaviour {

    private Animator anim;
    public float inputX;
    public float inputZ;
    public float lastInputX;
    public float lastInputZ;

	// Use this for initialization
	void Start () {
    renderer.castShadows = true;
    anim = GetComponent<Animator>();

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



        anim.SetFloat("speedX", inputX);
        anim.SetFloat("speedZ", inputZ);


	}

    void FixedUpdate()                                          //Fixed update function
    {

//        inputX = Input.GetAxis("Horizontal");
//        inputZ = Input.GetAxis("Vertical");

          inputX = Input.GetAxis("Horizontal");
          inputZ = Input.GetAxis("Vertical");

        if (inputX != 0 || inputZ != 0)
        {
            anim.SetBool("walking", true);
            if (inputX > 0)
            {
                anim.SetFloat("lastMoveX", 1f);
                lastInputX = 1;
            }
            else if (inputX < 0)
            {
                anim.SetFloat("lastMoveX", -1f);
                lastInputX = -1;
            }
            else
            {
                anim.SetFloat("lastMoveX", 0f);
                lastInputX = 0;
            }
            if (inputZ > 0)
            {
                anim.SetFloat("lastMoveZ", 1f);
                lastInputZ = 1;
            }
            else if (inputZ < 0)
            {
                anim.SetFloat("lastMoveZ", -1f);
                lastInputZ = -1;
            }
            else
            {
                anim.SetFloat("lastMoveZ", 0f);
                lastInputZ = 0;
            }
        }
        else
            anim.SetBool("walking", false);
    }
}

This works by animating on a freeform 2D using SpeedX and SpeedZ on the animation controller and it works fine.

And here is the way the enemies follow me:

using UnityEngine;
using System.Collections;

public class ScriptEnemyTrackPlayer : MonoBehaviour {

    public GameObject player;
    public int meleePower = 10;
    public float speed = 1.0f;
    public float attackSpeed = 1.0f;
    public float detectRange = 20.0f;
    public float attackRange = 4.0f;
    private float distance;
    private Vector3 delta;
    private float moveSpeed;

    public GameObject atLeft;
    public GameObject atRight;

    public float inputX;
    public float inputZ;
    public float lastInputX;
    public float lastInputZ;

    public bool attDirLeft;
    public bool attDirRight;

    private bool attackingLeft;
    private bool attackingRight;

    ScriptEnemyCollider enemyCollider;

	// Use this for initialization
	void Start () {

        player = GameObject.FindGameObjectWithTag("Player");
        rigidbody.freezeRotation = true;

        if (!player)
            Debug.Log("ERROR could not find Player!"); 

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

        inputX = Input.GetAxis("Horizontal");
        inputZ = Input.GetAxis("Vertical");

        if (!player)
            return;

        distance = Vector3.Distance(player.transform.position, transform.position);
        
        if ( distance < detectRange  )
        {
            delta = player.transform.position - transform.position;
            delta.Normalize();
            moveSpeed = speed * Time.deltaTime;
            transform.position = transform.position + (delta * moveSpeed);
        }
        else
        {
            //Debug.Log("player not close : " +distance);
        }

        if (distance <= attackRange && attDirRight == true)
           {
               StartCoroutine(playerAttackRight());
               playerAttackRight();
           }

        if (distance <= attackRange && attDirLeft == true)
           {
               StartCoroutine(playerAttackLeft());
               playerAttackLeft();
           }
	}

    void FixedUpdate()                                          //Fixed update function
    {
        lastInputX = Input.GetAxis("Horizontal");
        lastInputZ = Input.GetAxis("Vertical");

        if (lastInputX != 0 || lastInputZ != 0)
        {
            if (lastInputX > 0)
            {
                attDirLeft = false;
                attDirRight = true;
            }
            else if (lastInputX < 0)
            {
                attDirLeft = true;
                attDirRight = false;
            }
        }
    }

    public IEnumerator playerAttackLeft()
    {
        ScriptEnemyCollider enemyCollider = atLeft.GetComponent<ScriptEnemyCollider>();
        if (attackingLeft == false && attackingRight == false)
        {
            attackingLeft = true;
            atLeft.SetActive(true);
            atLeft.audio.Play();
            yield return new WaitForSeconds(attackSpeed);
            atLeft.SetActive(false);
            attackingLeft = false;
            enemyCollider.hit = false;

        }
    }

    public IEnumerator playerAttackRight()
    {
        ScriptEnemyCollider enemyCollider = atRight.GetComponent<ScriptEnemyCollider>();
        if (attackingRight == false && attackingLeft == false)
        {
            attackingRight = true;
            atRight.SetActive(true);
            atRight.audio.Play();
            yield return new WaitForSeconds(attackSpeed);
            atRight.SetActive(false);
            attackingRight = false;
            enemyCollider.hit = false;
        }
    }
    
}

The enemies use the exact same script for animations, just a different named script.

I cannot for the life of me understand why when I move, they all animate the same.

I have watched the controller, and its like the player input changes the values on the enemy script too, either that or my enemy follow script is somehow affected by the movement of the player?

If I stay still, the enemies all move towards me, but only display idle animation frames, like they aren’t moving.

Any help would be really appreciated!!

if ( distance < detectRange ) {
delta = player.transform.position - transform.position;
delta.Normalize();
moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}

Here it is. delta is not what you want to add to your enemy object. delta directs to player not the direction the player is moving.

Thanks very much for the help, I shall re-write the code :slight_smile: