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

Need very specific help with Quaternions

Discussion in 'Scripting' started by kzaurckichz, Dec 28, 2014.

  1. kzaurckichz

    kzaurckichz

    Joined:
    Nov 5, 2013
    Posts:
    52
    I need to make a character to always stand up against an artificial Gravity Vector.

    The artificial gravity source can be anywhere and any angle/vector.

    When I am inside the trigger collider of the gravity source, I want my character to slowly "stand up", meaning its feet pointing towards the floor, even if not touching, while keeping its rotation around its local Y axis to keep looking approximately in the same direction.

    I have a script attached to my character gameObject.

    By now I achieved to make the character to stand up against the global Vector3.up, I simply need help with doing it against a specific vector.

    Here's the part of this script I need help with :

    Code (csharp):
    1.  
    2.  Vector3 gravityVector = artificialGravitySource.transform.up; // unused for now...
    3.  
    4.  Quaternion startRotation = transform.rotation;
    5.  Quaternion endRotation = new Quaternion (0, transform.rotation.y, 0, transform.rotation.w);
    6.  
    7.  transform.rotation = Quaternion.RotateTowards(startRotation, endRotation, 100 * Time.deltaTime);
    8.  
    I need to set my endRotation according to gravityVector, keeping my local y rotation.

    Thank you for your help.
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    I think there different way to achieve that, i will try :
    Code (csharp):
    1.  
    2. // - init rotation movement
    3. Vector3 charDir = charDir.up;
    4. Vector3 targetDir = artificialGravitySource.transform.up;
    5. Quaternion targetRotation= Quaternion.FromToRotation(charDir ,targetDir );
    6.  
    7. // - rotation movement (in update function or a coroutine)
    8. transform.rotation = Quaternion.Lerp(transform.rotation,targetRotation, Time.time * speed);
    9.  
     
  3. kzaurckichz

    kzaurckichz

    Joined:
    Nov 5, 2013
    Posts:
    52
    This is not working for me... unless I don't understand how to implement it well..

    I need a very simple solution to rotate towards the gravity vector... Something like multiplying my endRotation with my gravityVector..