Simple Character AI

I am working to have a spawned character move towards a target. I can get the character to spawn, and move towards the target fine using the following code:

rigidbody.velocity = transform.forward * 10;
transform.LookAt(myCamera);

The only problem here is that it doesn’t follow the surface of my ground plane. This is for iOS, so using Terrain is not an option.

Sounds like you need to enable gravity on the rigidbody, or apply gravity yourself.

I like using Character Controllers instead of rigidbodies. Try using this:

var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var movespeed = 3.0;
controller.SimpleMove(forward * movespeed);

This will only move him forward. You’ll still have to use your own script to make him look towards the player. But this will give your enemy gravity and keep him from moving through walls.

you can use too an auxiliar vector to disable the y axis.

var cam:GameObject; // Atach here your camera in the inspector
var newpos:GameObject;
newpos.transform.position = Vector3(cam.transform.position.x,Y,cam.transform.position.z);
// change the Y by the y level of the gorund
rigidbody.velocity = transform.forward * 10;
transform.LookAt(newpos);

I solved this problem with a raycast which detect the surface and repositioned my character at the right height.