Can you make it where an object can't move along an axis without barriers? Even when acted on my a force?

I have an object, you can only move it along the X axis, but if you move it a lot, it will wobble and move along the Z axis. Here is a video to show what I mean.

https://youtu.be/CyrsZmt2dRg

I tried puting this code in the Update method and FixedUpdate method but no luck, I didn’t get errors for the code though

Vector3 Pos = rb.transform.position;
Quaternion Rot = rb.transform.rotation;


Pos.z = 0;
Rot.y = 0;

and I tried without putting rb, with no luck. I tried putting the code in a while loop, and unity froze :stuck_out_tongue:

I tried not to ask dumb questions, but I haven’t found any solutions, this is my first time using unity and C#.

2 Answers

2

You can use the rigidbody component to constrain the objects position and rotation. See the image below.

You probably want to constrain the z-axis position and the x- and y- axis rotation.

94123-rb-constraints.png

Yeah, I figured that out! But thank you anyway! I should have say that :P

Yeah, I figured that out! But thank you anyway! I should have say that :stuck_out_tongue: