Search Unity

Making movement relative to camera

Discussion in 'Scripting' started by lovemoebius, Apr 28, 2017.

  1. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    I'm not quite sure how to explain this but here I go.

    At the moment the movement for my character is relative to the input received by the analog stick, so an horizontal value and a vertical value.

    I followed a small guide to get a better camera in place, and, while I was able to get the camera working, I have absolutely no idea how to make my movement relative to it.

    I have a vector3 that always points at the opposite direction of the camera (moveDirection) and a vector3 that points to the front of the character.

    I supposed the solution would be to create some kind of angle between the two, but I'm not sure how to proceed from here.

    Any suggestions on what I should do?

    Here's my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5.  
    6. public class Controls : MonoBehaviour {
    7.     public float speed;
    8.     private Rigidbody rb;
    9.     Vector3 movement;
    10.     Vector3 movementRotation;
    11.  
    12.     public Vector3 jump;
    13.     public float jumpForce;
    14.     public float gravity;
    15.     public float jumpModifier;
    16.  
    17.     public ThirdPersonCamera gamecam;
    18.     private float directionSpeed = 3.0f;
    19.  
    20.     private float moveHorizontal;
    21.     private float moveVertical;
    22.     private float speedPlus = 0.0f;
    23.     private float direction = 0f;
    24.     private Vector3 moveDirection;
    25.  
    26.     public bool isGrounded = true;
    27.  
    28.     private Vector3 curLoc;
    29.     private Vector3 prevLoc;
    30.  
    31.     void Awake(){
    32.         rb = GetComponent<Rigidbody> ();
    33.         jump = new Vector3(0.0f, 2.0f, 0.0f);
    34.         Physics.gravity = new Vector3(0, -gravity, 0);
    35.     }
    36.        
    37.     void OnCollisionStay() {
    38.         isGrounded = true;
    39.     }
    40.  
    41.     void FixedUpdate () {
    42.         ControllPlayer();
    43.         StickToWorldspace (this.transform, gamecam.transform, ref direction, ref speedPlus);
    44.     }
    45.  
    46.     void ControllPlayer()
    47.     {
    48.         moveHorizontal = Input.GetAxis ("Horizontal");
    49.         moveVertical = Input.GetAxis ("Vertical");
    50.         Debug.Log (direction);
    51.         movementRotation = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    52.  
    53.         if (isGrounded == true) {
    54.             // Current movement
    55.             rb.velocity = new Vector3 (moveHorizontal, 0f, moveVertical).normalized * speed;
    56.         } else {
    57.             rb.AddForce (movementRotation * speed * jumpModifier);
    58.         }
    59.  
    60.         if (Input.GetKey (KeyCode.Space) || Input.GetKey (KeyCode.Joystick1Button1) && isGrounded){
    61.             rb.velocity = new Vector3 (rb.velocity.x, 12.0f, rb.velocity.z);
    62.             isGrounded = false;
    63.         }
    64.  
    65.  
    66.         if (movementRotation.sqrMagnitude > 0.1f && isGrounded){
    67.             transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movementRotation), 1F);
    68.         }
    69.  
    70.     }
    71.  
    72.     public void StickToWorldspace(Transform root, Transform camera, ref float directionOut, ref float speedOut){
    73.  
    74.         Vector3 rootDirection = root.forward;
    75.  
    76.         Vector3 stickDirection = new Vector3 (moveHorizontal, 0, moveVertical);
    77.  
    78.         speedOut = stickDirection.sqrMagnitude;
    79.  
    80.         Vector3 CameraDirection = camera.forward;
    81.         CameraDirection.y = 0.0f;
    82.         Quaternion referentialShift = Quaternion.FromToRotation (rootDirection, Vector3.Normalize(CameraDirection));
    83.  
    84.         moveDirection = referentialShift * stickDirection;
    85.         Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);
    86.  
    87.         // Will always face opposite to the camera
    88.         Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 5f, root.position.z), moveDirection, Color.green);
    89.         //Direction the player is facing
    90.         Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 5f, root.position.z), rootDirection, Color.magenta);
    91.  
    92.         // Creates Angle between the two
    93.         float angleRootToMove = Vector3.Angle (rootDirection, moveDirection);
    94.  
    95. //        angleRootToMove /= 180f;
    96.  
    97.         directionOut = angleRootToMove;
    98.         Debug.Log (directionOut);
    99.     }
    100. }
    101.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This is a common question. The key line is here:
    Code (csharp):
    1.  
    2.         movementRotation = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    3.  
    If you alter the value of movementRotation at that point, you can make it match the camera.
    The simplest thing to do is to apply the camera's rotation to it after the above line:
    Code (csharp):
    1. movementRotation = Camera.main.transform.rotation * movementRotation;
    This should get you close to the right movement. If your camera faces downward at all (as most cameras do) and you don't want that downward angle to "push" your character into the floor, you'll want to do something a little more complex, which is to make a "flattened" camera rotation before applying it to your vector:
    Code (csharp):
    1. Vector3 camForward = Camera.main.transform.forward;
    2. camForward.y = 0f;
    3. Quaternion camRotationFlattened = Quaternion.LookRotation(camForward);
    4. movementRotation = comRotationFlattened * movementRotation;
     
  3. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    That fixed it, thank you!