- Home /
Trying to clamp the movement of a rigidbody2D
I'm trying to limit movement to inside a certain area.
In 3d Unity I could do this (from the Project: Space Shooter):
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
However, rigidbody2D doesn't have a .position. I tried using transform but I am clearly confused on some part of it as I was getting an error from it.
Answer by Cellamusteen · Feb 22, 2014 at 06:14 AM
I replaced the code you have above with this (while using rigidbody2D):
transform.position = new Vector3 (
Mathf.Clamp (transform.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (transform.position.z, boundary.zMin, boundary.zMax));
Hope that helps.
Your answer
Follow this Question
Related Questions
Transform.position.x jumps to high numbers after collision 1 Answer
Clamping "X" Position Movement On Platform (Code Inside) 2 Answers
How to get a object moving straight to follow the y position of a object that is arching? 0 Answers
How to clamp gameobject movement in a irregular shape 1 Answer
clamping player position while in cover 0 Answers