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

Question How to get input relative to camera of player relative to player?

Discussion in 'Scripting' started by retsetera_unity, Jun 30, 2023.

  1. retsetera_unity

    retsetera_unity

    Joined:
    Jun 16, 2023
    Posts:
    2
    So, I am working on a project and I am trying to get input relative to camera of player relative to player. For example, if the player is facing right in perspective to the camera and the "D" (Right) button is pressed, then it needs to think the "W" (Forward) button is pressed so it moves forward. Another example is if the player is facing toward the camera, if the "W" (Forward) key is pressed, it needs to think the "S" (Backward) key is pressed.I already have code which stores the look direction of the camera, which by the way is a cinemachine free look, and sets a gameobject's transform's forward to it :

    Code (CSharp):
    1.  
    2. lookDirection = (cinemachineCamera.GetComponent<Cinemachine.CinemachineFreeLook>().LookAt.transform.position - cinemachineCamera.transform.position);
    3.         orientation.forward = new Vector3(lookDirection.x, 0f, lookDirection.z);
    4.  
    and I already have the input stored in movement.z for forward/backward and movement.x for left/right

    I have been having trouble with this for a while and I couldn't figure out a solution.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    You are thinking about this backwards. For some reason you're thinking about it in terms of changing which button is pressed? "f the player is facing toward the camera, if the "W" (Forward) key is pressed, it needs to think the "S" (Backward) key is pressed"

    All you need is to reorient the input vector according to how the camera is facing. The current facing of the player is not relevant to figuring that part out.

    Something like this should suffice:
    Code (CSharp):
    1. Vector3 cameraForward = Camera.main.transform.forward;
    2. Vector3 flattened = Vector3.ProjectOnPlane(cameraForward, Vector3.up);
    3. Quaternion cameraOrientation = Quaternion.LookRotation(flattened);
    4.  
    5. // now rotate the input vector:
    6. movement = cameraOrientation * movement;
    7. // Now you can move the player according to the movement vector.
    8. // This is just a stupid example since I don't know how your character controller works:
    9. transform.position = transform.position + movement * speed * Time.deltaTime;
     
  3. retsetera_unity

    retsetera_unity

    Joined:
    Jun 16, 2023
    Posts:
    2
    the reason I need this to happen is because in a wallrunning script it wall runs if W is pressed but I want it to also wall run if he is going forward in comparison to the key inputs and the camera orientation
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    For wall run you just project the camera forward direction along the surface normal. Again the player's orientation is irrelevant.