x


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").

more ▼

asked Apr 15 '10 at 06:24 AM

Lypheus gravatar image

Lypheus
292 28 35 49

Mark the answer as correct so that people can see this question is answered.

Apr 15 '10 at 06:42 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Apr 15 '10 at 06:49 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

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.

Apr 15 '10 at 04:06 PM Lypheus
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 03 '10 at 04:46 PM

Lypheus gravatar image

Lypheus
292 28 35 49

(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Aug 15 '10 at 01:20 PM

Robotron18 gravatar image

Robotron18
113 3 5 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2156
x1783

asked: Apr 15 '10 at 06:24 AM

Seen: 3378 times

Last Updated: Apr 15 '10 at 06:50 AM