How to move enemy with physics based movement?

Fairly new to Unity/C#. I'm doing an FPS. I've gotten all the animations down, but when it comes to actually having the enemy move, I'm having some issues. I'd like a physics based locomotion so the creature doesn't just clip through walls. I'm aware of Rigidbody,AddForce and CharacterController.Move but I have no idea how to "phrase" them given my current setup or what values to put where.

I've tried the AddForce page, but again, I'm unsure how to phrase it in a way, so that when the Walking animation starts, and the enemy has spotted the player, that it moves toward them.

1 Reply

Is there a reason your anim variable is static? This means that all instances of "WastrelChase" will share the same Animator component, which surely won't end well. I would recommend removing the static. keyword and replacing it with private, so it becomes

private Animator anim;

Back to the question, you need a reference to your Rigidbody. You can add it to the top of the script and assign it just like your anim component:

Depending on your enemy type, it may be a good idea to lock the X and Z rotation of the Rigidbody component, to prevent it from rolling around.

Next is to apply the force to the rigidbody. AddForce should be used in the FixedUpdate function, so what we will do is create a new Vector3 variable in our MonoBehaviour called "moveDirection", which we will use in the Fixed update function.

the 50f is the movement force. You will need to adjust it to get movement working right. If it doesn't appear to be moving, try increasing this to very large numbers (e.g. 50000f) to see if its just due to the force being to low.

thanks for your help, but the enemy is still shuffling in place, even with the force at 500000, is there something i should set my rigidbody / collider as in Unity? it doesn't appear to be affected by gravity either.