- Home /
How do I get character to always face mouse?
My character is on the XZ plane, this is a topdown 2D game. All that I want is that the character always faces the mouse, this shouldn't be that hard. Maybe I need to retake geometry, but this is actually REALLY confusing, can somebody help me out here? It shouldn't be that hard to make the character rotate to face the mouse (on the Y axis, I believe)..
Answer by Seizure · Jul 30, 2013 at 07:06 PM
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log ("hit point " + hit.point);
gameObject.transform.LookAt(hit.point);
}[link text][1]
[1]: /storage/temp/13725-lookat.zip
The issue is that I don't have a plane as the ground, I have a tilemap..eek
Actually this code and above code work with anything you use as the ground.
Answer by Seth-Bergman · Jul 30, 2013 at 06:53 PM
like this:
I assume you have a plane representing the ground, with a collider.. then just attach the following to your character (untested)
function Update () {
var hit : RaycastHit;
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit) {
transform.LookAt(hit.point);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Enemy collision in 2D top down 2 Answers
2D Game - Y axis rotation (left - right) 2 Answers
How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer
I'm attempting to make a character face the joystick in a top down unity2d game. 1 Answer