• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image studenyfilip · Apr 13, 2021 at 08:41 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.rotation and localRotation automatically reset after rotating. 1 Answer

Camera/Direction Rotation 2 Answers

How to move camera up and down 1 Answer

Getting 3rd person to look in direction of camera when right mouse clicked 1 Answer

How to change object direction when Camera Rotates 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges