Movement + Animation

I am trying to create a gameobject that moves towards a “base” object.

I will be using a SWITCH statement to change the state of the object.

When it is in the state that makes it move towards the base, it play’s it’s walk animation, and moves twords the base.

Here is the script thus far:

Code:

var base : Transform;
var state: String = “Idle”;
var speed : float = 0.05;

function Update ()
{

switch (state)
{
    case "Idle" :
         print("Idling.......");
    break;
    
         case "SEARCHING_FOR_TARGET":
    
         transform.position = Vector3.Lerp (transform.position, base.transform.position, Time.deltaTime * speed);
    
         targetRotation = Quaternion.LookRotation (base.transform.position - transform.position);
         str = Mathf.Min (2* Time.deltaTime, 1);
         transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
    
         rigidbody.AddForce (Vector3.forward * 20);
         animation.Play("MonsterWalk1");
    
         print("Looking for the base....");
    break;
    
    default:
         print("I have no state!!! I'm Stateless!!!!");
    break;
}

}

The problem I am having is that when I play the animation, it will not move. If I disable the animation, it moves as intended.

I of course want it to both play the walking animation, and move towards the base. What am I doing wrong? Something simple I presume?

You can decouple the game object controlled by animation from the gameobject controlled by code.

Just create a gameobject that your code modifies (via transform) and let your current game object (with animation component) be a child to this.

The animation then only controls the child game object (hierarchy) and you can transform it all around via the parent game object.

Hope it helps.