- Home /
How to Clamp X-axis Movement?
Hey guys,
I'm currently working on a game where I want the player to constantly move upwards (Y-Axis), I am having trouble trying to figure out how to clamp the X-axis only. Can anyone help me figure this problem out? At the moment, this is the script I wrote to stop the player from moving outside the boundaries:
rigidbody2D.position = new Vector2
(Mathf.Clamp (rigidbody2D.position.x, GameAttributes.ScreenMinimumWidth, GameAttributes.ScreenMaximumWidth), 0);
Thank you guys for the help - Beeraj
Is the problem that the object can't move on the y-axis in this script? In that case, ins$$anonymous$$d of using 0 as the y value, use rigidbody2D.position.y
Answer by F3RILLA · Aug 17, 2014 at 09:55 PM
The problem has now been fixed, thanks to Razacx - this was the solution:
Is the problem that the object can't move on the y-axis in this script? In that case, instead of using 0 as the y value, use rigidbody2D.position.y
Your answer
![](https://koobas.hobune.stream/wayback/20220613160834im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clamping rigidbody's Z position causing issues in the editor, when transform hits zero on Z axis 0 Answers
A node in a childnode? 1 Answer
Change player movement 0 Answers
Making character dash forward 0 Answers
2D rigidbody movement & walls 2 Answers