• 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 ialinen22 · Jun 17 at 05:38 PM · 3rd person controller3rd person cameraactions

How would I make it where the character moves where its facing based on camera?

Im using Input Actions which I'm not too familiar with. How would I make it where the character moves where he is looking?

 PlayerInput playerInput;
 CharacterController characterController;
 Animator animator; 

 Vector2 currentMovementInput;
 Vector3 currentMovement;
 bool isMovementPressed;
 public float rotationFactoredPerFrame = 1f;
 public Transform camera;

 float turnSmoothVelocity;

 void Awake()
 {
     playerInput = new PlayerInput();
     characterController = GetComponent<CharacterController>();
     animator = GetComponent<Animator>();

     playerInput.CharacterControls.Move.started += OnInputMovement;
     playerInput.CharacterControls.Move.canceled += OnInputMovement;
     playerInput.CharacterControls.Move.performed += OnInputMovement;

 }

 void HandleRotation()
 {
    /* Vector3 postionToLookAt;

     postionToLookAt.x = currentMovement.x;
     postionToLookAt.y = 0;
     postionToLookAt.z = currentMovement.z;
     
     Quaternion currentRotation = transform.rotation;

     if(isMovementPressed)
     {
         Quaternion targerRotation = Quaternion.LookRotation(postionToLookAt);
         transform.rotation = Quaternion.Slerp(currentRotation, targerRotation, rotationFactoredPerFrame * Time.deltaTime);
     } */

     if(isMovementPressed)
     {
         float targetAngle = Mathf.Atan2(currentMovement.x, currentMovement.z) * Mathf.Rad2Deg + camera.eulerAngles.y;
         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, rotationFactoredPerFrame);
         transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
     }

 }

 void OnInputMovement(InputAction.CallbackContext context)
 {
     currentMovementInput = context.ReadValue<Vector2>();
     currentMovement.x = currentMovementInput.x;
     currentMovement.z = currentMovementInput.y;
     isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
 }

 void HandleAnimation()
 {
     bool isWalking = animator.GetBool("isWalking");
     bool isRunning = animator.GetBool("isRunning");

     if(isMovementPressed && !isWalking)
     {
         animator.SetBool("isWalking", true);
     }
     else if(!isMovementPressed && isWalking)
     {
         animator.SetBool("isWalking", false);
     }
 }

 void Update()
 {
     HandleAnimation();
     HandleRotation();
     characterController.Move(currentMovement * Time.deltaTime);
 }

 void OnEnable()
 {
     playerInput.CharacterControls.Enable();
 }

 void OnDisable()
 {
     playerInput.CharacterControls.Disable();
 }

}

Comment
Add comment · Show 2
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 Venividiviciuus · Jun 17 at 05:58 PM 0
Share

I assume from how I see you have a Third Person Controller. It is not clear what you mean, do you want it to rotate together with the camera or do you want it to rotate only if the camera is angled and the player if moved gets the rotation of the camera angle?

avatar image ialinen22 Venividiviciuus · Jun 17 at 06:06 PM 0
Share

The best way I can explain is it that the camera is always directly behind the player when no input is given. I want the character to also move that direction if only W is pressed. but what's happening is that the forward is always same it does not move based on where the character is looking

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Venividiviciuus · Jun 17 at 06:06 PM

So I post my code where you can get both functions by removing the condition unless you want to I know i used rigidbody so i think you can use Slerp to rotate

 //the position where the camera look (so about the head)
 lookPos = new Vector3(transform.position.x, transform.position.y + lookHeight, transform.position.z);
 
             _xRotation += Mouse_X * sensX * Time.smoothDeltaTime;
             _yRotation += -Mouse_Y * sensY * Time.smoothDeltaTime;
 
             _yRotation = Mathf.Clamp(_yRotation, limitUp, limitDown);
 
             Vector3 Direction = new Vector3(0, 0, -distance);
             Quaternion rotation = Quaternion.Euler(_yRotation, _xRotation, 0);
             _mainCamera.transform.position = lookPos + rotation * Direction;
 
             _mainCamera.transform.LookAt(lookPos);
 
             //remove this condition if you want to rotate always not only when moved
             if (_inputManager.Move != Vector2.zero)
             {
                 _playerRigidbody.MoveRotation(Quaternion.Euler(0, _xRotation, 0));
             }
Comment
Add comment · Show 4 · 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 Venividiviciuus · Jun 17 at 06:20 PM 0
Share

THIS

 Quaternion newRotation = Quaternion.Euler(0, _xRotation, 0));
    transform.rotation = Quaternion.Slerp(
                                transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
 
avatar image ialinen22 · Jun 17 at 06:24 PM 0
Share

I think the problem for me is that I dont know how to get AxisRaw from currentMovement.x and z

avatar image Venividiviciuus ialinen22 · Jun 17 at 06:39 PM 1
Share

Create a Vector2 of your inputs of the inputsystem and normalize it

so

 Vector2 _inputDirection = new Vector2(_inputManager.Move.x, _inputManager.Move.y).normalized;


other I advice you to create another script with all the references of the InputSystem like this https://pastebin.com/R20XJkK7 if you need other stuff ask me ;)

remember to declare after in your code the references of the new script

  private InputManager _inputManager;  //in the variables
     
     _inputManager = GetComponent<InputManager>(); //in the Start/Awake
 
avatar image ialinen22 Venividiviciuus · Jun 17 at 07:37 PM 0
Share

Ok 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

139 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

Related Questions

Controlling a 3rd Person 2D Sprite in a 3D Environment 0 Answers

How to get JUST the y rotation of an object? 3 Answers

How to make the character move and rotate where the camera is facing (3rd person) 0 Answers

Change camera angle for 3rd person controller 1 Answer

How to make player move according to camera's rotation 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