Restrict camera to X/Z?

I got a simple camera script for my top-down 3d game. I like the camera movement, but I don’t want it to rotate while it follows the character, simply move along 2 axis’s.

For example, picture a 2D pixel game. Possible?

public class CameraTrack : MonoBehaviour {

    // Makes MainCamera follow character
    public Transform target;
  
    void Update () {
        transform.LookAt(target);
    }
}

Thanks in advance.

public class CameraTrack : MonoBehaviour {

// Makes MainCamera follow character
public Transform target;

void Update () {
Vector3 endPosition = new Vector3(target.position.x, transform.position.y, target.position.z);
transform.position = endPosition;

}

}


Not tested but should work. It will update the camera object position based only on the X and Z of the target. Will keep the actual camera Y.
1 Like

Has some issues, but it works and I’ll try to work out the bugs. Thanks!