Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Solved- How to rotate player using Dot product of two vectors

I got it working, I was overcomplicating things by using the Mathf.Cos function, because the Dot product is the cosign of the angle, so I’m not sure what i was thinking. here is the working code for anyone curious.

camRotation = player.transform.rotation.x + player.transform.rotation.y * Input.mousePosition.x + Input.mousePosition.y;
        player.transform.rotation = Quaternion.Euler(0, 0, camRotation);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

From what I can see from your question, you are trying to spin the player based on the current mouse position. However, there are a couple of issues with your approach:

  1. You should use Quaternion.Slerp rather than Quaternion.Lerp, as lerp simply interpolates each field and is almost never what you are looking for. Quaternion.Slerp moves as if spinning a sphere
  2. Unnecessary cosine calculation – Quaternion.Euler takes an angle, so just using the mouse position as the angle should acheive the desired effect
  3. Spinning on the wrong axis – you probably meant to use the y-axis rather than the z-axis

This is probably what you are looking for:

camRotation = Input.mousePosition.x;
player.transform.rotation = Quaternion.Slerp(Quaternion.Euler(0, player.transform.rotation.y, 0), Quaternion.Euler(0, camRotation * 5, 0), 1 * Time.deltaTime);

Please comment if I have misinterpreted 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading