Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
1 capture
14 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 Venividiviciuus · 16 hours ago · cameraplayerrotatedirection

Rotate player in direction of camera

I know there are countless threads on this topic but after hours of research I haven't found anything I need. So I wanted to ask you, how can I rotate the player in the direction of the camera?

using RotateTowards the player rotates according to an angle in speed * time but each rotation angle is different from each other and therefore the step it performs means that sometimes the player does not get a complete rotation towards the camera. so what I'm looking for is to instantly rotate the player in the direction of the camera when it moves

"precise I want the player to rotate only if the camera is at a different angle and when moved it goes in that direction" not that by rotating the camera the player rotates simultaneously this is my reference code.

             _xRotation += Mouse_X * sensX * Time.smoothDeltaTime;
             _yRotation += -Mouse_Y * sensY * Time.smoothDeltaTime;
 
             _yRotation = Mathf.Clamp(_yRotation, upperLimit, bottomLimit);
 
             Vector3 Direction = new Vector3(0, 0, -distance);
             Quaternion rotation = Quaternion.Euler(_yRotation, _xRotation, 0);
             _mainCamera.transform.position = lookPos + rotation * Direction;
 
             _mainCamera.transform.LookAt(lookPos);
 
             if (_inputManager.Move != Vector2.zero)
             {
                 //I would like to get rid of all this code flow 
                 Vector3 movement = new Vector3(_inputManager.Move.x, 0, _inputManager.Move.y);
                 Vector3 camDirection = _mainCamera.transform.TransformDirection(movement);
                 camDirection.y = 0;
                 camDirection.Normalize();
 
                 Vector3 desiredForward = Vector3.RotateTowards(transform.forward, camDirection, RotationSpeed * Time.smoothDeltaTime, 0f);
                 transform.rotation = Quaternion.LookRotation(camDirection);
                 //up to here
             }
         }  
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
0

Answer by Rocco_Taco · 16 hours ago

Hello I am not sure if this helps but here is the code i used. pretty much when rotation the player you want the camera to move up and down only but when turning left to right you want the player to move as well. Under your player in the hierarchy add an empty object (as a child of player) and call it orientation. Ignore the wall run part they are just values for something else.

    [Header("Refrences")]
 [SerializeField] WallRun wallRun;

 [Header("Sens")]
 public float sensX;
 public float sensY;

 public Transform orientation;

 float xRotation;
 float yRotation;

 // Start is called before the first frame update
 void Start()
 {
     Cursor.lockState = CursorLockMode.Locked;
     Cursor.visible = false;
 }

 // Update is called once per frame
 void Update()
 {
     float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
     float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

     yRotation += mouseX;
     xRotation -= mouseY;

     xRotation = Mathf.Clamp(xRotation, -90f, 90f);

     transform.rotation = Quaternion.Euler(xRotation, yRotation, wallRun.tilt);
     orientation.rotation = Quaternion.Euler(0, yRotation, 0);

Not sure if this will work but under your 7th line add

orientation.rotation = Quaternion.Euler(0, xRotation, 0);

and add a transform:

public Transform orientation;

Hope this helps! GL

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

234 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

problem with rotate camera around player 0 Answers

How to Rotate Camera around GameObject (Player) 0 Answers

Change characters direction using camera view 0 Answers

How to make camera not rotate with player, only move 1 Answer

How to don't face player to camera direction? 1 Answer


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