Search Unity

Help me understand the character relative to camera script

Discussion in 'Scripting' started by GTHell, Jun 26, 2019.

  1. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    I've seen this snippet below being mention/refer everywhere(Youtube & Forum post) but I dont' understand it. I just copy and paste it because, heck, I don't know what it even means.

    I can't find someone to come up with a better explanation.

    Code (CSharp):
    1.  
    2.  
    3.         float horizontalAxis = CrossPlatformInputManager.GetAxis("Horizontal");
    4.         float verticalAxis = CrossPlatformInputManager.GetAxis("Vertical");
    5.        
    6.         var camera = Camera.main;
    7.  
    8.         var forward = camera.transform.forward;
    9.         var right = camera.transform.right;
    10.        
    11.         forward.y = 0f;
    12.         right.y = 0f;
    13.         forward.Normalize();
    14.         right.Normalize();
    15.        
    16.         var desiredMoveDirection = forward * verticalAxis + right * horizontalAxis;
    17.        
    18.         transform.Translate(desiredMoveDirection * speedMeUp * Time.deltaTime);
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Lines 3-4 get keyboard input and store it in those 2 variables. Each will be from -1 to 1.

    Lines 8-9 get two direction vectors, one representing the camera's forward and one representing the camera's right.

    Lines 11-14 flatten both of these vectors (so they're moving along the ground, and angling the camera downward won't change them) and normalize them (so the length of these vectors is forcibly set to 1)

    Line 16 is the magic line. It multiplies the camera's flattened forward vector by the vertical movement. Basically, when player presses up, verticalAxis is 1, and this is multiplies by the vector we have to become a "camera forward" vector. When it's -1, this produces a "camera backward" vector. Then we do the same thing with the horizontal and add them together.

    Line 18 is just applying this movement to the transform.
     
  3. GTHell

    GTHell

    Joined:
    Jan 5, 2016
    Posts:
    256
    In line 16, why the sum of the forward * verticalAxis & right * horizontalAxis? That's confusing. Can you possibly explain the math behind that?
     
  4. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    The world of your game has it's own coordinate system. This is worldspace, it never rotates. For example, for the world forward is always the same: the direction you see towards when you open a new scene.
    BUT your character's forward is independent and it'll depend on what direction your character is facing. Rotate around and you'll see your forward change. That is your "local" forward.
    For this reason every transform has its own forward direction, a right direction and an up direction (that you're not using here). This have been set by Unity to help you define a position with respect to your character's position. Usually what you do is multiply them. For example, Transform.forward * 25, will give you 25 units infront of the player.
    Now, what xhe's doing in that line, is multiplying the input form the keys pressed by the player. For example, when I press the left arrow (i.e. horizontal axis), I want the character to move to its left. My desired position is to the left of the player, so, to get that position, I just grub my character transform.right, and I subtract whatever the input has been ( I subtract because -right = left ). The axis will automatically give positive or negative values. for example if a press the down arrow, this will return a negative value.
    If instead the player presses the up key a lot (i.e. positive vertical axis), this means he wants to go forward, so I multiply the input to the forward character's vector.
    The player may be pressing buttons of both the horizontal and vertical axis, so the desire position may be a mix between the 2 (a diagonal), that's why we add both. If the player is not pressing one of those, the input will give 0, and 0*forward vector is 0, so it adds nothing and doesn't bother.
    With this 3 vectors (forward, right & up) you can define any position realtive to your character.
     
    Last edited: Jun 27, 2019