- Home /
Character Rotation point at Mouse
Greetings,
In my game my character is saw from a top-view camera. I want my character rotation to alway point toward the mice.
So I think I'd need to have the component which indicate where my mice is in World Position rather than in GUI position.
Read mouse with Input.mousePosition. Get it to world position with Camera.ScreenToWorldPoint and then set your character Y rotation with something like Quaternion.LookRotation(target - current)
Care to elaborate?
I tried some methodes using this and it doesn't face the mouse, it moves... But doesn't face the mice using the methode Script Reference is using.
Answer by cj_coimbra · Dec 08, 2011 at 09:34 PM
In the test scene I put a cube and a plane under it. I pointed the camera downward to simulate your top-down view. I also tagged the plane as "Plane" (duh)... Worked fine here but you will have to make adjustments as you will probably have other objetcs other than the floor (represented as the Plane in this example). But this should help you find your way.
private Vector3 lookRotationPoint;
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.tag == "Plane")
{
lookRotationPoint = hit.point - transform.position;
transform.rotation = Quaternion.LookRotation(lookRotationPoint.normalized);
transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
}
}
}
Oh, and this Update() goes in a script attached to the object you want to rotate.
Work, I only need to translate it to JS and I'm set. Thanks.