Search Unity

Help moving character using cameras .Up and .Right

Discussion in 'Scripting' started by Chance-Touchstone, Oct 11, 2018.

  1. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
    I'm trying to build a (3D) game where my character can walk up walls and on ceilings. And I need to change the direction the character moves based on the camera's current rotation. Assuming my camera is always facing the correct direction and view/rotation... how can I use the camera's transform.up and transform.right to determine which way the Horizontal and Vertical input move a character?

    For example, I want the W and S keys to move the character Up and Down parallel to the camera's current .Up direction. And the A and D keys to move the character Right and Left using the camera's current .Right direction.

    How would you script that?!
     
    Last edited: Oct 12, 2018
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Just spit-balling here and don't have unity open to test..

    Thinking logically from 2D first.
    Transform directions are world space.
    So if the player is facing forward ">", transform.Forward would be (0,1)
    If the camera was rotated 270 degrees "^", trandform.Forward would be (1,0)

    I think you would want to store the players forward as a variable,
    Get the angle difference between the player and camera, being 270.
    Then rotate rotate that stored direction by 270
    That would be the move direction.

    If 2D works, it will be easy to work it out in 3D