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)

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.

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)