Search Unity

How do I rotate a Vector3 direction by another direction

Discussion in 'Scripting' started by Parappa, Jul 31, 2014.

  1. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    I want to rotate a Vector3 by the grounds normal. How would I do this? Vector.jpg
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
  3. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    That not what im looking for at all
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1.  
    2.     void UpdateRotation()
    3.     {
    4.         RaycastHit hit;
    5.         if(Physics.Raycast(transform.position, -transform.up, out hit, 10))
    6.         {
    7.             transform.rotation = Quaternion.LookRotation(hit.normal);
    8.         }
    9.     }
    ? O: <
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Vector3.right is always the same direction.
    So don't confuse me by changing it's angle in the drawing ;-;
     
    rakkarage likes this.
  6. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    I'm making a Vector 3 from the players input and need it rotated by the normal the player is standing on
    So I can apply force on the player that is consistent with the slope
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Are you in a 2D game or a 3D game? This is important because in 3D you have the z component to think about which means you can rotate into or out of the screen.

    In 2D, you need to determine which way you need to rotate the normal.
    Code (csharp):
    1. Vector3 normal = someNormal;
    2. // Rotate around z axis, Correct if we are moving left.
    3. Vector3 rotatedLeft = Quaternion.Euler(0f, 0f, 90f) * normal;
    4. // Correct if we are moving right.
    5. Vector3 rotatedRight = Quaternion.Euler(0f, 0f, -90f) * normal;
     
    ffxiangyu likes this.
  8. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    It's 3D
     
  9. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Check the slope using raycast, then move the player relative of that rotation.
     
  10. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    That not what im trying to do at all
     
  11. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Don't you want to move & rotate the player RELATIVE to the normal beneath the player? e__o
     
  12. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
  13. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Can you visualize it? x_x
     
  14. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    I'll try
     
  15. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    The player pressed the right key, so a blue line comes out to the right, level with the ground
    BUT the cube has nothing to do with it, the cube can be at any rotation

    The player can also press 2 keys together like forward and sideways to go diagonal
    vector3.png
     
  16. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    And the blue line indicates the direction that it's translating towards?
     
  17. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    The blue line is just a blue line for now
    Its shooting out to the right because the player pressed the right key
     
  18. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I'm going to sleep, I'll try to help u tomorrows
     
  19. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    Alright
     
  20. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    I found the answer here
    http://answers.unity3d.com/questions/10323/calculating-a-movement-direction-that-is-a-tangent.html

    Code (CSharp):
    1.     Vector3 surfaceNormal; // Get the normal value on collision;
    2.     Vector3 myDirection; // player direction in X-Z space;
    3.    
    4.     void FixedUpdate(){
    5.     Vector3 temp = Vector3.Cross(surfaceNormal, myDirection);
    6.     Vector3 myDirection = Vector3.Cross(temp, surfaceNormal);
    7.     }
    surfaceNormal is the raycast hit.normal
    and myDirection is the player input
     
    Magiichan likes this.
  21. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    This is the real correct answer. The one above only works properly on flat ground

    Vector3 tiltInput = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
    Vector3 projForward = transform.forward - (Vector3.Dot(transform.forward, hit.normal) * hit.normal);
    Vector3 rayDirection = Quaternion.LookRotation(projForward, hit.normal) * tiltInput;
    rayDirection.Normalize();

    Debug.DrawRay(transform.position, rayDirection * 10, Color.magenta);
     
  22. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    This works too:

    Code (CSharp):
    1.  
    2. Vector3 tiltInput = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    3. // Vector3 projForward = transform.forward - (Vector3.Dot (transform.forward, hit.normal) * hit.normal);
    4. Vector3 rayDirection = Quaternion.LookRotation (transform.forward, hit.normal) * tiltInput;
    5. rayDirection.Normalize ();
    6.  
    7. Debug.DrawRay (transform.position, rayDirection * 10, Color.magenta);
    8.  
     
    Last edited: Aug 10, 2014
  23. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    This also works but it's relative to the world x, z axis (but perhaps you dont care):

    Code (CSharp):
    1.  
    2. Vector3 tiltInput = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    3. Vector3 rayDirection = Vector3.Cross (hit.normal, tiltInput);
    4. rayDirection = Vector3.Cross (rayDirection, hit.normal); // you can remove this line if you swap "Horizontal" and "Vertical"
    5. rayDirection.Normalize ();
    6.  
    7. Debug.DrawRay (transform.position, rayDirection * 10, Color.magenta);
    8.  
     
  24. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    In the answer you found, the last was this:

    Code (CSharp):
    1.  
    2. Vector3 tiltInput = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    3.  
    4. float y = (-hit.normal.x * tiltInput.x - hit.normal.z * tiltInput.z) / hit.normal.y;
    5. Vector3 rayDirection = new Vector3 (tiltInput.x, y, tiltInput.z);
    6.  
    7. rayDirection.Normalize ();
    8. Debug.DrawRay (transform.position, rayDirection * 10, Color.magenta);
    9.  
     
  25. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    This one shoots along the angle of the player on some slopes instead of the angle of the slope

    This one doesnt do anything at all on some slopes, the direction will stay the same no matter your rotation
     
  26. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    This one does not do anything some times too


    This is still the best answer
    Code (CSharp):
    1. Vector3 tiltInput = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
    2. Vector3 projForward = transform.forward - (Vector3.Dot(transform.forward, hit.normal) * hit.normal);
    3. Vector3 rayDirection = Quaternion.LookRotation(projForward, hit.normal) * tiltInput;
    4. rayDirection.Normalize();
    5.  
    6. Debug.DrawRay(transform.position, rayDirection * 10, Color.magenta);
     
  27. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    It seems you did a very deep test... I only tryed on the same slope. There was another solution after the one I posted in the answer thread (the very last): did you tested it too?
     
  28. Parappa

    Parappa

    Joined:
    Jan 27, 2013
    Posts:
    77
    Yes I tried that one aswell, doesnt work.
    Wonky on the x axis, gives shortened direction or starts pointing into the sky/floor
    may have given the wrong z axis aswell dont remember
     
    Last edited: Aug 10, 2014