Ignoring the transform.y position on movement

For example, I have 2 gameobjects. I have a script where gameobject1 follows gameobject 2 using this function:

controller.SimpleMove(transform.forward * speed);

This works fine, but when I add more gameobjects, that also moves towards gameobject 1, it starts bugging out on the Y position, resulting the gameobject starts to fly above gameobject 1.

Is there some way to make sure every gameobject always stays on the ground (transform.position.y = 0)?

Since you are using SimpleMove, gravity is applied. So my guess is that your problem come from the lookAt which apply a rotation on the x and/or z axis. But you probably only want to look forward the direction in which your moving (you only want rotation on the y axis).

So you have two possibilities:

  1. You only keep the rotation on y axis:

    Vector3 rot = player.eulerAngles;
    rot.x = 0;
    rot.z = 0;
    player.eulerAngles = rot;

  2. Or you can use Vector3.RotateTowards, in which you also can “gradually” rotate towards ur target:

    float step = speed * Time.deltaTime;

    Vector3 from = new Vector3(transform.position.x,0,transform.position.z);
    Vector3 to = new Vector3(player.position.x,0,player.position.z);
    transform.eulerAngles = Vector3.RotateTowards(from,to,step,0.0f);

If there is a rigidbody attached, either through code or the inspector you can freeze rotation and position.

RigidBody Constraints