So I have a an NPC set up with an AI to wonder. I cannot figure out how to animate it. I have a player controller for my player but, its based on keyboard inputs to animate the player, there is no keyboard input for the wondering character. I am having a hard time animating it.
i don't think it going to work out the way you wanted like that. I made a character script which contains my animate movement. Then my enemy, npc and player script inherits from it so they all share the same animate movement code. My movement input in the player script and input manager script. Check out unity Inscope rpg tutorial. It helped me a lot.
You will need your NPC to have a rigidbody component. Design Methods for the directions: Code (CSharp): private Rigidbody2D npc; // If the script is on you NPC this will be okay //If not, consider serializing the field or making it public. private bool canMove; private void Start() { npc = GetComponent<Rigidbody2D>(); canMove = true; } private void Update() { // You will call your functions here. // However, you want to define your conditions of movement // in perhaps an IENumerator or use ray casting. // if your raycast detects a wall, make your enemy change direction // otherwise, if your NPC is freeroam you can use an IENumerator // to let it move for a certain amount of time then change direction. if(canMove == true) { StartCoroutine(NPCMovement()); } } void MoveLeft() { npc.velocity = Vector2.left; //Include you animation and other relevant sequences here } void MoveRight() { npc.velocity = Vector2.right; //Include you animation and other relevant sequences here } A demonstration of IENumerator: Code (CSharp): IENumerator NPCMovement() { canMove=false; MoveLeft(); yield return new WaitForSeconds(5f); MoveRight(); yield return new WaitForSeconds(5f); canMove=true; }