x


How to stabilize angular motion (alignment) of hovering object?

How do I stabilize the angular motion of a hovering object? I have a hovering vehicle that moves at some distance above the ground, controlled with the arrows keys. The problem is that when it collides with some object (like a building) its rotation gets shifted. When I am moving it after this, it moves according to its local space rotation.

How can I cause my Rigidbody to return back to its initial rotation where it is only rotated around the Y axis?

more ▼

asked Jan 21 '10 at 01:32 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

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

2 answers: sort voted first

You can use this script (C#) to stabilize your Rigidbody so it returns to an alignment where the local Y axis points upwards:

using UnityEngine;
using System.Collections;

public class HoverStabilizer : MonoBehaviour {

    public float stability = 0.3f;
    public float speed = 2.0f;

    // Update is called once per frame
    void FixedUpdate () {
        Vector3 predictedUp = Quaternion.AngleAxis(
            rigidbody.angularVelocity.magnitude * Mathf.Rad2Deg * stability / speed,
            rigidbody.angularVelocity
        ) * transform.up;

        Vector3 torqueVector = Vector3.Cross(predictedUp, Vector3.up);
        rigidbody.AddTorque(torqueVector * speed * speed);
    }
}

With the script you can control both how fast the Rigidbody stabilizes and how stable or wobbly the stabilization is.

Note: If you only want to stabilize along one axis, you can project the torqueVector onto that axis. For example, to stabilize along the local z (forward) axis only, do this before applying the torque:

torqueVector = Vector3.Project(torqueVector, transform.forward);
more ▼

answered Jan 21 '10 at 01:34 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

Thanks, This worked great for me. You are a legend!

Dec 19 '10 at 02:49 AM YoshieMaster

This worked beautifully for me thanks!

Dec 10 '12 at 07:45 AM PraetorBlue

Works perfectly for me, thank you !

Mar 31 at 06:55 PM again
(comments are locked)
10|3000 characters needed characters left

Thank you so much . I am still quite new to 3d math in unity so i am having a hard time to figure out this stuff . Before your solution I actually decided to use Configurable Joint and cancel out angular x,z but the downside of it was that on collisions there was no angular change at all that have made these collisions to look unrealistic . Now that is exactly what I need .

Thanks once again !!!!!!!

more ▼

answered Jan 21 '10 at 01:54 PM

Sasmaster gravatar image

Sasmaster
89 10 12 16

You're welcome! :) Consider in the future to add comments like this using "add comment" to the answer you're commenting on, rather than adding it as a new answer (because your comment is not an answer to the posted question).

Jan 21 '10 at 04:23 PM runevision ♦♦
(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:

x2249
x1948
x1336

asked: Jan 21 '10 at 01:32 PM

Seen: 3886 times

Last Updated: Mar 31 at 06:55 PM