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

Cam Transform Vector confusion???

Discussion in 'Scripting' started by shotoutgames, Sep 12, 2014.

  1. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    283
    Can someone please explain the following code snippet to me ??
    More specifically the creation of the right Vector
    Code (CSharp):
    1.  
    2.         Vector3 forward = mainCam.transform.TransformDirection(Vector3.forward);//forward of camera to forward of world
    3.         forward.y = 0f;
    4.         forward = forward.normalized;
    5.         Vector3 right = new Vector3(forward.z, 0.0f, -forward.x);
    6.         Vector3 targetDirection = (horizontal * right + vertical * forward);
    7.         if (targetDirection.sqrMagnitude > 1f)
    8.         {
    9.             targetDirection = targetDirection.normalized;
    10.         }
    11. //Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up) * camRotation;
    12.   Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
    13.   // Create a rotation that is an increment closer to the target rotation from the player's rotation.
    14.   Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, slerpDampTime * Time.deltaTime);
    15.  
    The target direction is the gamepad right stick input.
    The script allows the gamepad to stay oriented with the rotation of the camera but I do not understand why the right vector was created this way ( - forward.x ??? )
    I have tried to map it out pen paper and tracing source but it is not clicking :(
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    It's a quick trick to create a vector that is perpendicular to a given vector in 2D.

    If we calculate the dot product of vectors (x, y) and (y, -x), we'll get zero

    (x, y) dot (y, -x) = x*y + y*(-x) = x*y - x*y = 0

    which means the vectors are perpendicular to each other.
     
    Last edited: Sep 12, 2014
  3. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    283
    Thank you.
    What would be an equivalent way to code this without using tricks? Can we use a cross product ? Why is this needed anyway? Just trying to understand completely but no matter thanks.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Its just to effectively rotate the forward vector by 90 degrees
    You could go the hard way and create a 90 degree rotation around the up axis, and rotate the vector
    Ot you could cross the forward vector with up
    But why bother when you can just swap the x and z values. Its not a bad trick.
     
    shotoutgames likes this.
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    1. If we didn't know the trick, we could draw a picture and "guess" the same formula from it:
    WP_001022.jpg

    2. Or we could solve it analytically:
    WP_001023.jpg
    which gives us two vectors (y, -x) and (-y, x) perpendicular to (x, y), one looks to the right and another to the left.

    3. I personally would use my own helper methods that use cross product:
    Code (csharp):
    1. public static Vector3 CalculateUp(Vector3 forward, Vector3 right)
    2. {
    3.     return Vector3.Cross(forward, right).normalized;
    4. }
    5.  
    6. public static Vector3 CalculateForward(Vector3 right, Vector3 up)
    7. {
    8.     return Vector3.Cross(right, up).normalized;
    9. }
    10.  
    11. public static Vector3 CalculateRight(Vector3 up, Vector3 forward)
    12. {
    13.     return Vector3.Cross(up, forward).normalized;
    14. }
    4. Or we could rotate the forward vector:
    Code (csharp):
    1. Vector3 right = Quaternion.Euler(0, 90, 0) * forward;
    ------------------------------
    Code (csharp):
    1. Vector3 targetDirection = (horizontal * right + vertical * forward);
    Horizontal and vertical variables, I believe, are taken from the player's input. The forward axis is taken from the camera and the right axis is calculated from it.

    Actually, if the camera doesn't roll, I think we could just take the right axis from the camera as well:
    Code (csharp):
    1. Vector3 right = mainCam.transform.right;
    And even if it rolls, we could use the same trick as with the forward axis:
    Code (csharp):
    1. Vector3 right = mainCam.transform.right;
    2. right.y = 0;
    3. right.Normalize();
     
    Last edited: Sep 13, 2014
    shotoutgames likes this.