Hi im kinda new to this. im trying to store what direction my Zombie enemy is walking in terms of x(1 or -1) and y(1 or -1) so i can use it for it’s animation. im doing a top down game.
here is the code that i use to move it towards my player
public class ZombieAI : MonoBehaviour {
Rigidbody2D rbody2d;
Animator anim;
Transform moveDir;
public Transform target;
public float moveSpeed;
private float range;
private float moveDir_x;
private float moveDir_y;
// Use this for initialization
void Start () {
rbody2d = GetComponent ();
anim = GetComponent ();
moveDir = GetComponent ();
}
// Update is called once per frame
void Update () {
MoveToPlayer ();
}
//Move towards the player
public void MoveToPlayer(){
range = Vector2.Distance (transform.position, target.position);
if (range > 0) {
Debug.Log (range);
transform.position = Vector2.MoveTowards (transform.position, target.position, moveSpeed * Time.deltaTime);
}
}
}
and i want to be able to set the animations in a blend tree like i have done with my player using this code
public class PlayerController : MonoBehaviour {
Rigidbody2D rbody2d;
Animator anim;
public float moveSpeed = 5f;
// Use this for initialization
void Start () {
rbody2d = GetComponent ();
anim = GetComponent ();
}
// Update is called once per frame
void Update () {
Vector2 movement = new Vector2 (Input.GetAxis (“Horizontal”), Input.GetAxis (“Vertical”));
if (movement != Vector2.zero) {
anim.SetBool (“isWalk”, true);
anim.SetFloat (“input_x”, movement.x);
anim.SetFloat (“input_y”, movement.y);
} else {
anim.SetBool (“isWalk”, false);
}
rbody2d.MovePosition (rbody2d.position + movement * moveSpeed * Time.deltaTime);
}
}
if anyone can help it will be greatly appreciated ^^