- Home /
self righting object
Working on getting an object to self right, firstly the object is rotated to a random Vector3, then it should self right.
function Selfright () {
var pointUp = Quaternion.RotateTowards(gameObject.transform.rotation, upobj.transform.rotation, Time.deltaTime * 40);
gameObject.transform.rotation = pointUp;
}
Unfortunately this will make the object point in the Z direction of the upobj, all I really need to do is make the object self right. (point up over a duration of time)
Do you want to rotate the object to a random point during some time, then return to the previous direction?
here's the random rotation code:
gameObject.transform.rotation = Quaternion.LookRotation(Vector3(Random.value,Random.value,Random.value));
Answer by aldonaletto · Nov 26, 2011 at 04:59 AM
You could use physics to emulate this "inflatable punching bag" behaviour - but instead of applying the weight to the bottom, you could apply a constant up force to the top with AddForceAtPosition. The script below does exactly this effect with a capsule with rigidbody:
var force: float = 5; // up force var offset: float = 1; // offset from object's position (up direction)
function FixedUpdate(){ var point = transform.TransformPoint(offset Vector3.up); rigidbody.AddForceAtPosition(force Vector3.up, point); } You can tweak the effect with offset: the higher it is, the faster the object will self right. You can also increase the Drag factor in the rigidbody to make it stop swinging in less time.
Answer by Nonakesh · Nov 22, 2011 at 08:39 PM
I'm currently not on my PC, so I can't test if this works, but you should try this:
function Selfright () {
var pointUp = Quaternion.RotateTowards(gameObject.transform.rotation, Quaternion.LookRotation(upobj.transform.up), Time.deltaTime * 40);
gameObject.transform.rotation = pointUp;
}
If you don't want to set a custom up direction you could replace it with Quaternion.LookRotation(Vector3.up)
Thanks for the feedback, seems this approach makes the objects Z axis (front) point up. Also the object aligns it's self so that it's X (left, right) is perfectly rotated to 0 (Z is rotated to zero)
The effect I'm looking for is more like one of those inflatable punching bags, it right's it's self by having a weight in the bottom, but isn't limited in what directions any of the faces point at.
$$anonymous$$aybe you could try to change only x and y, so the left-right rotation still stays the same!
I tried something along those lines. Was more of a hack really.
Your answer
![](https://koobas.hobune.stream/wayback/20220613051953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Rotate Raycast on Quaternion 1 Answer
Perpendicular Vector3 0 Answers
Quaternion amount/value of rotation? 2 Answers
How to know if a gameobject is rotating? 1 Answer
Quaternion.Euler rotations don't add up 0 Answers