- Home /
 
               Question by 
               studenyfilip · Apr 12, 2021 at 03:19 PM · 
                camerarotationdirection3rd person controller3rd person camera  
              
 
              How to move player in direction where the camera is aiming ?
Hello, everybody.
So I am trying to create a 3rd person controller. Ihave a camera controller and player movement, but I can't find out how to make the player move in the direction of where the camera is facing.
Player movement script
 public class PLAYER_Movement : MonoBehaviour
 {
     public int walkingSpeed, runningSpeed;
     public int movementSpeed;
     public float turnSmoothTime;
 
     public bool isSprinting;
    
 
     //Jump Stuff
     Vector3 velocity;
     public float gravity = -9.8f;
     public Transform groundCheck;
     public float groundDist;
     public LayerMask groundMask;
     bool isGrounded;
     public float jumpHeight = 3;
 
     CharacterController charController;
     public Transform playerCamera;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         charController = GetComponent<CharacterController>();    
     }
 
     // Update is called once per frame
     void Update()
     {
         Movement();
     }
 
     void Movement() 
     {
         float inputX = Input.GetAxisRaw("Horizontal");
         float inputZ = Input.GetAxisRaw("Vertical");
         isSprinting = Input.GetKey(KeyCode.LeftShift);
 
         Vector3 moveDir = new Vector3(inputX, 0, inputZ).normalized;
         movementSpeed = isSprinting ? runningSpeed : walkingSpeed;
 
 
         Jumping(moveDir);
 
         //Rotate player in direction of camera
         transform.rotation = Quaternion.Euler(0f, playerCamera.eulerAngles.y, 0f);
 
         Vector3 moveAmount = moveDir * movementSpeed;
         velocity.y += gravity * Time.deltaTime;
         moveAmount.y = velocity.y;
 
         charController.Move(moveAmount *  Time.deltaTime);
     }
 
     void Jumping(Vector3 moveDir) 
     {
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDist, groundMask);
         if (isGrounded && velocity.y < 0)
         {
             velocity.y = -1f;
         }
 
         if (Input.GetKeyDown(KeyCode.Space) && isGrounded && moveDir.magnitude <= 0.05f)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
 
         }
     }
 }
and this is my camera controller script
 public class PLAYER_CameraController : MonoBehaviour
 {
     public float rotationSensitivity;
     public Vector2 cameraInput;
     public Vector2 maxMinRotation;
 
     public Vector3 cameraPosition_NOaim;
 
     public Transform playerTarget;
     public Camera playerCamera;
 
 
     private void Update()
     {    
         RotateCamera();   
     }
 
     void RotateCamera() 
     {
         cameraInput.y += Input.GetAxis("Mouse X") * rotationSensitivity;
         cameraInput.x += -Input.GetAxis("Mouse Y") * rotationSensitivity;
         cameraInput.x = Mathf.Clamp(cameraInput.x, -maxMinRotation.x, maxMinRotation.y);
     }
 
     private void LateUpdate()
     {
         Vector3 dir = cameraPosition_NOaim;
 
         Quaternion rotation = Quaternion.Euler(cameraInput.x, cameraInput.y, 0);
         rotation.x = Mathf.Clamp(rotation.x, -maxMinRotation.x, maxMinRotation.y);
         transform.position = playerTarget.transform.position + rotation * dir;
         transform.LookAt(playerTarget.transform.position);
     }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by HellsHand · Apr 12, 2021 at 08:26 PM
You could make the camera a child of the player and set it's position in the inspector then use something like:
 void Movement()
 {
     isSprinting = Input.GetKey(KeyCode.LeftShift);
     Vector3 moveAmount = Vector3.zero;
     movementSpeed = isSprinting ? runningSpeed : walkingSpeed;
     Jumping(moveAmount);
     velocity.y += gravity * Time.deltaTime;
     moveAmount.y = velocity.y;
     moveAmount.z = Input.GetAxis("Vertical") * movementSpeed;
     moveAmount.x = Input.GetAxis("Horizontal") * movementSpeed;
     Vector3 direction = transform.TransformDirection(moveAmount);
     transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * mouseSensitivity, 0);
     charController.Move(direction * Time.deltaTime);
 }
This would also negate the need for the second script on the camera.
That is actually good idea, but I will stick to what I have, you did helped me with my problem. I totaly forgot that .TranformDirection exists, with that I have manged to fix my problem. Thank you.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                