- Home /
Question by
JannickL · Dec 10, 2017 at 12:04 PM ·
camerarotationquaternioncamera rotatelook
Rotate player to screen pos
Hey, this is for a top down game.
I want the player to always face the point where the mouse is (without use of raycast).
Got the code working for a controller but not for mouse:
private void RotatePlayer() {
Vector2 mousePos = Camera.main.ScreenToViewport(Input.MousePosition);
float angle = Mathf.Atan2(mousePos.x, mousePos.y) * Mathf.Rad2Deg;
Quaternion desiredRot = Quaternion.AngleAxis(angle, transform.up);
rb.rotation = Quaternion.Lerp(rb.rotation, desiredRot, rotationSpeed * Time.fixedDeltaTime);
}
Actually I think the calculation of the angle is wrong. How can I correctly get the angle of the mouse and an object in 3D space (viewport)?
Any suggestions? Greets
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612144320im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Top down camera for object moving on sphere 1 Answer
How to make 90 degree snapping camera, center on object? 0 Answers
Camera.WorldToScreenPoint Equivalent 1 Answer
move the object where camera look 0 Answers
View Matrix Camera rotation problem 0 Answers