Trying to create a 2d Top down random movement. I have the blendtree setup so that it faces the sprite in the right direction. However, I can’t get it move right. It just flips back and forth between directions never moving. Unless I use the RidgidBody2d apply force then it just slowly convulses to 0,0.
I guess I wanted it walk in one direction base cardinal direction (no angles) for 3 seconds then change direction.
What Am I doing wrong?
public float moveSpeed = 0.75f;
public Animator anim;
public Rigidbody2D body;
private Vector2 enemyMovement;
void Start () {
anim = GetComponent<Animator> ();
}
void Update () {
Invoke("randomDirection", 1);
transform.position = new Vector3(enemyMovement.x,enemyMovement.y,0) * moveSpeed * Time.deltaTime;
//TRIED THIS TOO
//body.AddForce (enemyMovement);
}
void randomDirection() {
switch(Random.Range (0,3)){
case 0:
Debug.Log ("Moving enemy Up");
enemyMovement = new Vector2 (0,1);
break;
case 1:
Debug.Log ("Moving enemy Right");
enemyMovement = new Vector2 (1,0);
break;
case 2:
Debug.Log ("Moving enemy Down");
enemyMovement = new Vector2 (0,-1);
break;
case 3:
Debug.Log ("Moving enemy Left");
enemyMovement = new Vector2 (-1,0);
break;
default:
enemyMovement = new Vector2 (0,0);
break;
}
anim.SetBool ("Walking", true);
DefineFacingDirection (enemyMovement);
}