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 :

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 🙂

Leave a Reply