Hi, all! I am almost done with my first game and I need just this answer and I will be done! YAY!
i have a ship in my game that moves only on the x and y axes. I would like to make it so that the ship cannot leave the screen (or any boundary I have set up) like how in Ping-Pong the paddles cannot leave the board.
Yeah it should work, but it all depends on the view of the camera, if you are using a perspective view then you need to make sure you replace camera.nearClipPlane with the transform.position.z as objects further away are allowed to be much higher than objects that are closer.
If itâs ortho, then you should be fine with the script above.
No, restricting the motion of an object is too specific a feature to be built into a general-purpose game engine such as Unity. What restricting an objectâs motion means exactly will vary from game to game; as such, itâs really up to the developer to implement features like this.
As for your problem, how are you moving the object? Is it a rigid body? Are you using the character controller component?
No and no. I am simply making if statements on Input.GetAxis and then having my ship move up, down, left, or right * Time.deltaTime*speed (a variable i set)
Just consider it to be pseudocode, and adapt it as needed for your language of choice. (If you run into problems, tell us what language youâre using and what problems youâre encountering.)
Probably doesnât do what you want. First of all, Iâm not sure what Mathf.Clamp() does when the input range is inverted (that is, min > max). Also, thereâs likely little point in clamping âxâ to a range thatâs relative to âxâ. (My earlier example shows how youâd clamp âxâ to a set range.)
That said, it looks like youâre using colliders, which suggests that you may not want to be moving your object using Translate() anyway.
As far as I know, that code will work as is in UnityScript (aka javascript). However, I donât use UnityScript myself so I canât say for sure. (You could just try it out and see if it works though. Or, maybe someone else can confirm its correctness.)
Edit: Just to be clear, the code Iâm referring to (and the code Iâm assuming youâre referring to), is this code: