Hey,
asyou can see in the title, the animation from an enemy starts when i press W or A and move the player.
I don’t really know where the problem is and hope that you can help me.
Sorry for my bad english
Thanks for helping me.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
bool facingRight = false;
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int minRange;
private Transform myTransform;
Animator anim;
// Use this for initialization
void Awake() {
myTransform = transform;
}
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
anim = GetComponent<Animator> ();
target = go.transform;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 dir = target.position - myTransform.position;
dir.z = 0.0f; // Only needed if objects don't share 'z' value
if (dir != Vector3.zero) {
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.FromToRotation(Vector3.right, dir), rotationSpeed * Time.deltaTime);
}
if(Vector3.Distance(transform.position,target.position)<minRange)
//Move Towards Target
myTransform.position += (target.position - myTransform.position).normalized * moveSpeed * Time.deltaTime;
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}