- Home /
How to limit a rigitbody to an area
I have a game object (say a cube) with a rigitbody, so it can be pushed around by the player. This game object is the child of another Cube (lets call it field). I want to limit the inner object so it cannot be pushed outside of the field.
My problem arises when the field is rotated compared with the global axis. At this point I cannot simply use Mathf.Clamp, since the global x and z coordinates do not have a constant upper/lower boundary (the boundaries for x depend on the current z and vice versa). I tried clamping transform.localposition but this has rather odd effects and doesn't seem to work.
I could use trig to calculate the boundaries in regards to the current position on the other axis ( the boundaries for x =[field.position.x -(tan(a) (cube.position.z - field.position.z)),field.position.x + (tan(a) (cube.position.z - field.position.z))]) but that does seem like a sort of messy workaround.
Is there a relatively simple way to restrict the cubes movement to inside the field?
Answer by Jaimi · Sep 11, 2017 at 04:41 PM
Put up blocks around the area. Turn off their renderers. Adjust the collision layer parameters so that only the block is affected by the collision.
Your answer
Follow this Question
Related Questions
How to clamp X axis rotation of a GameObject with a Rigidbody attached. 0 Answers
How to Clamp X-axis Movement? 1 Answer
Controlling velocity of rigidbody in air 0 Answers
Area setting when moving rigidbody 0 Answers
Clamping rigidbody's Z position causing issues in the editor, when transform hits zero on Z axis 0 Answers