- Home /
restricting movement along an axis
I'm relatively new at programming, and find that Java is the simplest for me. I'm looking to keep a game object within a certain field of movement to stay inside the view of the camera without messing with a lot of barrier planes. I set the movement with the mouse, and set the requirements within the if statement to be that the position has to be between X.X on the X-axis and X.X on the Y-axis. This is successful but when it hits the edge it no longer lets me move the object in-game. Anyone got a fix?
Unity uses Javascript, not Java; they're two different program$$anonymous$$g languages.
Answer by Loius · Oct 12, 2010 at 02:22 AM
You can use
x = Mathf.Clamp( x, 0.0, 5.0 );
If x is below 0.0, it will be set to 0.0; above 5.0, will be set to 5.0; otherwise, it's unchanged.
Answer by BurningThumb · Aug 28, 2010 at 03:25 AM
I assume your logic that states that you can only move if you are within the bounds specified, when you reach them your script probably disables the ability to move because you've told the program to do that. You really should just use collisions, thats what they are there for.
If your implementation looks like this :
if (inBounds)
{
mouvement logic;
}
else
{
return;
}
Try this instead
if (inBounds)
{
mouvement logic;
}
else
{
move player back into bounds;
}
so I should just use planes and dde-render them but put colliders on them? Is that what you're saying? Like, the code is an alternative to colliders?
Your answer
![](https://koobas.hobune.stream/wayback/20220613071952im_/https://answers.unity.com/themes/thub/images/avi.jpg)