- Home /
Question by
RAINV7 · Nov 10, 2017 at 01:44 PM ·
mousepositionaim
While holding down the mouse button make object rotate to mouse cursor
I am making a 3D top shooter game and I want the player to be able to while holding down the left mouse button it aims towards the mouse, and it doesnt stop until you release the mouse button. the problem I have is that the character only rotates towards the mouse when you first click the button, but it doesnt stay aimed at the mouse when holding down it.
this is my script:
#pragma strict
var aim: Vector3;
function Update()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, 100))
{
aim.x = hit.point.x;
aim.z = hit.point.z;
}
if (Input.GetMouseButtonDown(0))
{
transform.LookAt(transform.position + aim);
}
}
Comment
Best Answer
Answer by Dragate · Nov 10, 2017 at 01:52 PM
Replace
GetMouseButtonDown
with
GetMouseButton
From documentation of GetMouseButtonDown():
The state gets reset each frame. It will not return true until the user has released the mouse button and pressed it again.