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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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:
    26
    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,687
    Have you tried simply:
    Code (csharp):
    1. RedArrow.rotation = Quaternion.LookRotation(red);
     
    Yoreki likes this.
  3. curtispelissier

    curtispelissier

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

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,687
    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:
    26
    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:
    26
    Ok, I found that setting the forward directly with my direction is working :

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