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

Completely rotate an element to a direction

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

  1. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    Hello everyone,

    I need to set the rotation of an element to the direction of a velocity Vector3. I have a XR Oculus Controller which I bind the "devicevelocity" with the New Input System

    upload_2020-4-3_17-33-38.png

    It works really well but I need to get that Vector3 and put it into an object's rotation. So I performed a Quaternion.LookRotation() with my Velocity as parameter but the Z axis of my element is always set to 0. (the "RedArrow" is the transform of my object to rotate).



    I also tried all those code, but they do not work as I would like :
    Code (CSharp):
    1. // Test 1
    2. RedArrow.rotation = Quaternion.LookRotation((transform.position + red) - transform.position);
    3.  
    4. // Test 2
    5. RedArrow.rotation = Quaternion.LookRotation(transform.position + red);
    6.  
    7. // Test 3
    8. RedArrow.LookAt(RedArrow.position + red);
    So how can I achieve to get my object rotate to the vector direction of my controller's velocity ?


    Configuration
    Unity Version : 2019.3.7f1
    Input System Package : preview6-1.0.0
    Oculus XR Plugin Package : 1.2.0
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Have you tried simply:
    Code (csharp):
    1. RedArrow.rotation = Quaternion.LookRotation(red);
     
    Yoreki likes this.
  3. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    Yes, I said it in my post, it was the first thing I tried.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Oh, I missed that, I only saw the ones below it in the code tags. Anyway with this:

    What other result would you expect? Or put another way, why are you considering it incorrect behavior that the Z is always 0?
     
  5. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    I'm expecting the object to "look at" the direction that I want (the controller velocity). And with the Z axis set to 0, the object is rotating on a plane, not to the real direction.
     
  6. curtispelissier

    curtispelissier

    Joined:
    Jul 26, 2019
    Posts:
    39
    Ok, I found that setting the forward directly with my direction is working :

    Code (CSharp):
    1. RedArrow.forward = red;
    Thanks for answers !