Building a app game using a space ship. The space ship the player can have move left and right. I’m having an issue with the space ship going off screen when moving left and right. my code is not working. The space ship goes off screen when moving left and right. I don’t want to place colliders on the sides since when my space ship hits colliders it spins. its a 2d game btw using 3d objects. Please let me know the best code for this. thanks for the help in advance!
Good day.
Add a RigidBody component. In the inspector, you wil lsee you can lock axis movement and axis rotations.
Bye!
Try something like this to restrict your ship’s horizontal movement by using Mathf.Clamp();
float minX = -10f;
float maxX = 10f;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), transform.position.y, transform.position.z);
Just figure out the farthest left and right positions you want your spaceship to be able to go to and set those for minX and maxX;