- Home /
The question is answered, right answer was accepted
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.
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 :P
I tried not to ask dumb questions, but I haven't found any solutions, this is my first time using unity and C#.
Well that code would of almost worked, but the Quaternion rotation and Vector3 position are struct fields of the Transform, you can't edit them outright with a reference. Just re-assign them afterwards like this.
Vector3 Pos = transform.position;
Quaternion Rot = transform.rotation;
Pos.z = 0;
Rot.y = 0;
transform.position = Pos;
transform.rotation = Rot;
Or a shorter version.
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 0, transform.rotation.w);
Also keep in $$anonymous$$d this won't account for any $$anonymous$$or losses in speed due to friction changes, any subtle collisions that may of caused you to wobbled will still effect Velocity, but won't alter your position, and may not be noticeable.
Answer by NoBrainer-David · May 15, 2017 at 09:46 AM
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.
Yeah, I figured that out! But thank you anyway! I should have say that :P
Answer by DerpyGamerYT · May 18, 2017 at 11:01 AM
Yeah, I figured that out! But thank you anyway! I should have say that :P