I’m trying to combine the implied camera perspective of classic 2D Zelda titles with the controls of twin-stick shooters, in 3D. I can move the player with the WASD keys, but I’m not sure how to rotate him to face the mouse cursor. I think I need to do the following:
- Calculate the cursor’s position on the screen.
- Draw a line between that position and the player character.
- Calculate the difference between the player character’s current Y axis rotation and that line.
- Rotate the player character on the Y axis by that difference, so that the difference becomes 0.
This way if the player moves the cursor to the top of the screen the character will look “North”; if the player puts the cursor in the bottom left corner, the character will look “Southwest”, if that makes sense.
I tried using transform.LookAt, but this caused a crazy feedback loop that made the character spin out of control:
Ray mouseRay = playerCamera.ScreenPointToRay(Input.mousePosition);
float midPoint = (transform.position - playerCamera.transform.position).magnitude * 0.5f;
transform.LookAt(mouseRay.origin + mouseRay.direction * midPoint);
I also tried the Brackey’s tutorial here, but was unable to calculate the lookDir because ScreenToWorldPoint
returns a Vector2, while a 3D object’s transform.position
is a Vector3.
Any ideas?