Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stopping sideways rotation

Discussion in 'Physics' started by petey, Mar 26, 2017.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    Hi Al!

    I am trying to stop this sideways drifting rotation -

    Locking the y rotation doesn't work that well because it's a global effect, so when the object hits the incline it gets messed up.
    Apart from that I have tried to apply an opposite rotation force locally but theres something not right about that (I think it's my script actually :( )

    Does anyone know how I could stop this?
    Thanks!
    Pete

    Oh and here's my script -
    Code (CSharp):
    1.     public Rigidbody RB;
    2.     public Transform ThisObj;
    3.  
    4.     void Awake(){
    5.         RB = this.GetComponent<Rigidbody>();
    6.         ThisObj = this.GetComponent<Transform>();
    7.     }
    8.  
    9.     void FixedUpdate () {
    10.         Vector3 LocalTorque = ThisObj.InverseTransformDirection(RB.velocity);//get local rotation
    11.         RB.AddRelativeTorque (new Vector3(0,-LocalTorque.y,0));//apply opposite to counter the roation
    12.     }
     
  2. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Try adding a configurable joint which can be configured in world or local space. Then lock the axis (of the joint not rigidbody) and see if that get's you closer to what you want.