Hey everyone,
I am trying to make a gun always point at the mouse cursor. The MouseLook script allows users to control the rotation of an object using the mouse, but the object doesn’t actually LOOK at the mouse cursor at all times…
This is my code to do this:
function Update() {
var screenPoint = Input.mousePosition;
transform.LookAt(Camera.main.ScreenToWorldPoint(screenPoint));
}
Unfortunately it doesn’t work.
Any ideas what I’m doing wrong?
fafase
2
How’s your camera? Are you in 3D? 2D? Perpendicular to the ground?
You could use:
var hit : RaycastHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
transform.LookAt(hit.point);
}
Edit:Sorry I edited 3 times in a row…