- Home /
Question by
addhennequant · Jul 30, 2012 at 03:39 AM ·
raycastmousedamage
Holding down the left mouse to continually damage an ai?
This is my script, but the raycast only damages the ai every click, i want it to damage the ai even when i hold down the left mouse button:
function Update(){
if(Input.GetMouseButtonDown(0)){ // if the Left button pressed
// casts a ray from the mouse pointer
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if(Physics.Raycast(ray, hit)){ // if something hit, send the message
hit.collider.SendMessageUpwards("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver);
}
}
}
Comment
Answer by Apples_mmmmmmmm · Jul 30, 2012 at 03:57 AM
You want to use GetMouseButton rather than GetMouseButtonDown.
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButton.html
Answer by cheesejunkie · Jul 07, 2015 at 10:41 PM
If you're using C#, you need the out keyword before hit: if(Physics.Raycast(ray, out hit))