- Home /
Question by
F3RILLA · Aug 17, 2014 at 09:49 PM ·
movementrigidbody2dclamp
How to Clamp X-axis Movement only?
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
Comment
Answer by Razacx · Aug 17, 2014 at 09:52 PM
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.
There wasn't any need to create a second question tough :p. You might want to remove your first.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160834im_/https://answers.unity.com/themes/thub/images/avi.jpg)