- Home /
Is there a way to keep a rigidbody upright at all times - to stop physics rotating it?
Can Rigidbody be used in place of casting rays for keeping a game object walking on top of uneven terrain? I'm just playing with it and it seems to be ok, the only problem so far is that my soldier now will topple over trying to go up a hill :). That makes sense to me since his underlying collision object is a box.
So my question is if there is a way to force a rigidbody to always stay in an upright position? I'm guessing there must be some way to override the physics engines updates to my object right? Or is there a way to assign 99% of a gameobjects weight/mass to the bottom of the object (i.e. feet) so that it will not be impacted by slopes etc?
To be specific, what I'm asking is how to lock this object into staying parallel with the world Y axis instead of perpendicular to the surface slope and to effectively behave in a IsKinemetic with regards to balance/angles - while not being IsKinemetic (which would mean having to cast rays and adjust y axis every movement it looks like to "stay grounded").
$$anonymous$$ark the answer as correct so that people can see this question is answered.
Answer by duck · Apr 15, 2010 at 06:49 AM
Yes there is a method to stop your rigidbody toppling over. You can set the "Freeze Rotation" property, either in the rigidbody's inspector values, or via scripting.
You can then manually rotate the object around the Y axis using Transform.rotate, and you can rely on the object remaining upright at all times.
Sounds great, wow I'm embarassed to say I even stared at the checkbox for freezing rotation and it just didn't click - bleh. Will try things out tonight here.
Answer by Lypheus · May 03, 2010 at 04:46 PM
I was looking at this the other night and decided on something which is pretty obvious to me now :) :
- Get the Vector3 that you want to travel to
- Modify the Y-axis so that it is the SAME as the object you are moving.
- LookAt the modified Vector3 instead of the original.
Now when you LookAt, it will of course look at the x,z coordinates and keep the same y as the current object which effectively keeps the object upright in its current context.
Answer by Robotron18 · Aug 15, 2010 at 01:20 PM
You can't use Transform.rotate or Transform.translate if used Rigidbody. http://unity3d.com/support/documentation/Components/class-Rigidbody.html You can use Rigidbody.MovePosition or Rigidbody.MoveRotation, but better to use Rigidbody.AddRelativeForce or Rigidbody.AddRelativeTorque for right work physics. And only into FixedUpdate().
Code for correction needless Rigidbody roration:
Quaternion rot = rigidbody.rotation;
rot[0] = 0; //null rotation X
rot[2] = 0; //null rotation Z
rigidbody.rotation = rot;
I'm having trouble understanding how this worked. This is some wizardry I'm unfamiliar with.
We get the rotation, we make an array of the rotation which is equal to zero, then we ooooh! so the quaternion is an array, I didn't understand that. and you're nullifying those axis'.
how in the world does it keep the object oriented upright? Unless by nullifying the x and z you essential spin it upright.
this has been very informative, but I need to be able to modify the torque value so I'll keep looking.
Thank you!
Answer by elinongags · Feb 28, 2017 at 01:28 PM
Freeze Rotation via this C# script...
using UnityEngine; using System.Collections;
public class FreezeRotation : MonoBehaviour {
float lockPos = 0;
// Update is called once per frame
void Update () {
transform.rotation = Quaternion.Euler (lockPos, lockPos, lockPos);
}
}
Thanks so much. This is a simple answer that was immensely helpful, as I'm just starting program$$anonymous$$g and needed a quick way to freeze rotation. Thanks!