What's the best move/rotate method to simulate a herd of animals moving around?

Since there are so many different methods of moving and rotating gameObjects in Unity, I can’t decide which one to use for this specific purpose without having too much performance cost.

I have 20-30 gameObjects that I want to keep moving towards random targets (at random directions) until they either reach the target or collide with another object at which point they get a new random target, smoothly turnand start moving towards it.

I believe that I definitely have to use non-kinematic rigidbodies and freeze their X and Z rotations, but I can’t decide which rigidbody methods I should use?!

Hey and welcome,

mostly there is no real best way to do X. However in your case i’d suggest the following:

Since you do not want to have physics based interactions between your animals and their environment (except for collisions) you should make them kinematic. Then use GetComponent().MovePosition(). For rotations MoveRotation(…). Why these functions? because with this (if called properly in FixedUpdate) you will get proper collision interactions. Just setting a transforms position/rotation will not give you the same result.

The last thing you should do for performance optimzation is cache your Rigidbody reference so that you do not call GetComponent each frame.

So, I ended up giving the non-kinenatic rigidbody a constant forward velocity for movement and setting the transform.rotation value directly in a Slerp coroutine for rotation.
I also removed

if (transform.position == targetPos) { NewTarget(); }

and only call the NewTarget() method in OnCollisionEnter, now.
However, I’ve discovered that every time OnCollisionEnter is called, I’m getting multiple Target positions, which ends up confusing the object rotation, but I guess I would have to create a new question for that.