I am trying to make a simple 3D Tank game, where the enemy tanks go from one random coordinates to another. When they spawn, they are aligned to the ground, like this
And most of them move around just fine, but sometimes they start rotating up when moving forward, and then they get like this, and start jumping up and down
To keep the LookAt method from affecting the x and z rotation, you can construct a new Vector3 with X and Z coordinates from the target and the Y coordinate from the enemy itself.
Vector3 targetVector = new Vector3(target.transform.position.x, this.transform.position.y, target.transform.position.z);
transform.LookAt(targetVector);
This will keep the tank from tilting when moving as the target always is on the same y-position as itself.
To keep it from jumping you could also lock the y-position of the rigidbody component, but i would suggest looking at wheel colliders instead. I think you can eliminate all these problems by adding “wheels” to your tank. Unity - Manual: Create a vehicle with Wheel Colliders