- Home /
Look at mouse cursor script not working properly
I've been trying to get the mouse cursor to register as a point in 3D space, so I can then have a character look at it. I don't want a mouse look system, where moving the mouse rotates the camera and the character, I want the character to actually look at the mouse cursor. I've tried casting a plane from the character and casting a ray and using where it intersects but that only works in a 2D environment with a static camera.
So next I figure a combination of ScreenPointToRay and Input.mousePosition is how to accomplish this. This is what I tried:
RaycastHit hit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)){
transform.LookAt(hit.point);
}
It kind of works. The character will only look at the mouse cursor if its under a certain height on the screen. Going above a certain height and he will ignore it. Also, if the mouse cursor goes on top of him, instead of following it in a forward direction, he'll actually turn around to look at it. What am I still missing?
It depends: what type of game are you making (i.e. is it a first-person or third-person game)?
The character will only look at the mouse cursor if its under a certain height on the screen. Going above a certain height and he will ignore it. Also, if the mouse cursor goes on top of him, ins$$anonymous$$d of following it in a forward direction, he'll actually turn around to look at it.
From what you said, it seems as though the camera is positioned to look at the ground. Is this correct?
It's a third-person game, with the camera behind the player