Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] How to set Local angular velocity?

Discussion in 'Scripting' started by EternalMe, Apr 3, 2020.

  1. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    181
    I found lot's of info hot to retrieve it:

    Code (CSharp):
    1.      Vector3 localAngularVelocity = target.transform.InverseTransformDirection(target.rigidbody.angularVelocity);
    2.  
    3. //localAngularVelocity.x is the target's Pitch,
    4.  
    5. //localAngularVelocity.y is the target's Yaw,
    6.  
    7. //localAngularVelocity.z is the target's Roll,
    8.  
    9. //assuming the target's forward is aligned with its z axis.
    But what I really want is to set local Pitch, Yaw, Roll and convert it to variables for
    Code (CSharp):
    1. gameObject.GetComponent<Rigidbody>().angularVelocity = ...
    to compliment the rolling direction. Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You can set it, but they do advise that it "can result in unrealistic behavior" in the docs:

    https://docs.unity3d.com/ScriptReference/Rigidbody-angularVelocity.html

    If you want to spin something at 1 rad / second down the Z axis, set the angularVelocity to Vector3.forward.

    The direction of the vector is the axis of the rotation, and the magnitude is the speed of rotation.

    Put this on a cube in your scene and watch the cube:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SpinMeWithAngularVelocity : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         var rb = gameObject.AddComponent<Rigidbody>();
    8.         rb.useGravity = false;        // don't fall
    9.         rb.angularVelocity = Vector3.forward * 1.0f;    // spin around +Z at 1rad/sec
    10.         rb.angularDrag = 0.0f;        // don't slow
    11.     }
    12. }
     
    anycolourulike likes this.
  3. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    181
    Your example doesn't really answer or solve my question. It still rolling in world space. And the link is totally different topic (about physical materials).
     
    anycolourulike likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    From where I stand it appears I linked you to angularVelocity. That doesn't have anything to do with physical materials but rather with ... wait for it ... the angularVelocity property on a Rigidbody.

    Good luck sir!
     
    anycolourulike likes this.
  5. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    181
  6. Duncro

    Duncro

    Joined:
    Dec 1, 2019
    Posts:
    8
    Is there something like this to get a "relative" angular velocity?
     
  7. coldstar

    coldstar

    Joined:
    Sep 20, 2014
    Posts:
    13
    rb.angularVelocity = Vector3.forward * 1.0f; // spin around +Z at 1rad/sec
    I think this line of code should be changed to:
    rb.angularVelocity = gameObject.transform.forward * 1.0f; // spin around +Z at 1rad/sec
     
  8. coldstar

    coldstar

    Joined:
    Sep 20, 2014
    Posts:
    13
    var localAV = Quaternion.Inverse(transform.rotation) * GetComponent<Rigidbody>().angularVelocity;
     
    gop and EternalMe like this.
  9. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    177
    //get Vector to local Space (even though angular velocity is no Vector3, but 3 values for x,y,z angular velocity)
    Vector3 localAngularVelocity = target.transform.InverseTransformDirection(target.rigidbody.angularVelocity);
    localAngularVelocity.z += 1; //local forward
    target.rigidbody.angularVelocity = target.transform.TransformDirection(localAngularVelocity); //transform it back

    what you actually should do to make it work better with Unity Physics, in case it fits you needs:

    Vector3 myAddedVelocity;
    target.rb.AddRelativeTorque(velocity * Vector3.forward, ForceMode.velocity); // instant change in velocity around local axis
    //should be same as:
    target.rb.AddTorque(velocity * target.transform.forward, ForceMode.velocity);
     
    PatHightreeXVR likes this.