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

How to rotate an object so it's Y axis points away from the world origin

Discussion in 'Scripting' started by FuzzyQuills, Nov 4, 2014.

  1. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Hello! :)

    I ask something: how would one go about generating a quaternion that makes the top of the object point away from the world origin?

    The quaternion will be used to rotate an object while instantiating it, as my game uses radial gravity (i.e: player and enemies walk around the surface of the planet) and needs the enemies and player to point towards the world origin when spawning (This is where the center of the planet is) so that the raycast used by my physics script picks up the planet's surface, and thus applies the proper gravity. At the moment though, my characters instantiate with what looks like the default rotation!

    Thanks for all the help given! :D
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    Assuming your models' local 'Y' vector is their up vector, couldn't you just determine their orientation based on the difference of where they're instantiated in world coordinates and the target origin, and set this new vector as their up vector?
     
  3. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Yes, my model (or, my parent object actually! :D) uses local Y direction for everything, as public Y obviously doesn't point outwards from the planet. o_O

    And wouldn't transformPoint (or the reverse) help with this? or not? :p

    And the way you have explained this tells me you're saying that the object's rotation can be generated by using Quaternion.LookRotation and the difference between (0,0,0) and <object position>. I have already tried this, and for some odd reason, it didn't work... :D
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok... I tried LookRotation again, it's sort of working, except the X axis, not the Y axis, is pointing at the origin! :eek:

    The heck's going on?! O.O
     
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok, fixed it!

    Set the up direction to, weirdly, the side... and it works! :)

    Will inform if something else goes horribly wrong... :D Also, I instantiate with default rot, and then I set it to the rotation I want after.
     
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I take back what I said... :D
    it still does it, and it's very inconsistent: it will do it correctly most of the time, but then at others, the vector is COMPLETELY WRONG! :eek:
     
  7. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    This script might help you.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestingUpVector : MonoBehaviour
    5. {
    6.  
    7.     public Transform characterTransform;
    8.     public Transform originTransform;
    9.     public float lookAroundDirection = 0f; // turn the character based on this value
    10.     public float rayDistance = 1.5f;
    11.  
    12.     void OnDrawGizmos()
    13.     {
    14.         if (characterTransform != null && originTransform != null)
    15.         {
    16.             if (enabled)
    17.             {
    18.                 Gizmos.color = Color.black;
    19.                 Gizmos.DrawLine(originTransform.position, characterTransform.position);
    20.                 Gizmos.color = Color.green;
    21.                 Gizmos.DrawRay(characterTransform.position, (characterTransform.right * rayDistance));
    22.                 Gizmos.color = Color.yellow;
    23.                 Gizmos.DrawRay(characterTransform.position, (characterTransform.up * rayDistance));
    24.             }
    25.         }
    26.         else Debug.Log("Either one or both Transforms are null for Monobehavior: TestingUpVector");
    27.     }
    28.  
    29.     // Use this for initialization
    30.     void Start()
    31.     {
    32.  
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         Logic();
    39.     }
    40.  
    41.     void Logic()
    42.     {
    43.         // These 3 lines describe how I'm thinking of your problem and how it can be solved
    44.         Vector3 resultUp = characterTransform.position - originTransform.position;
    45.         characterTransform.up = resultUp; // assign the up vector
    46.         Quaternion currentOrientation = characterTransform.localRotation; // find out what the character's  new local rotation is now after the assignment
    47.  
    48.         // this line is extra to show you can also change the direction your character is looking after instantiating
    49.         Quaternion turnRotation = Quaternion.Euler(0, lookAroundDirection, 0); // assuming we just want to look around us
    50.  
    51.         // Apply the rotations to the localRotation. You don't need turnRotation, it's just here to show you additional options.
    52.         characterTransform.localRotation = currentOrientation * turnRotation; // perform the look around rotation then the character-to-origin orientation rotations
    53.     }
    54. }
    55.  
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    You do realize I only need to set the vector once for instantiate function... :D

    But I should be able to integrate this into my project easily. but this line:
    Code (csharp):
    1. Vector3 resultUp = characterTransform.position- originTransform.position;
    Wouldn't one normalize this vector first? :p
     
  9. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    I'm aware of that. The script was strictly to show you what's going on in Realtime and that it doesn't matter where you place the Model relative to the origin transform - the up vector will be assigned with the correct value.
     
  10. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    LOL! :D:p:eek::rolleyes:

    It works, but I might have to reverse the position, because my character's head points downwards! :D

    Of course, I can fix this easily! :)
     
  11. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    Then either your models follow a -Y up rule, or you mixed up the character and origin transform parameters.

    Either way, happy coding!
     
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok, all fixed now! thank you for all your help! :) :D