- Home /
Input.GetMouseButton
Hi there i have a major problem. I have created a melee system using ray-casting and i need it to cast the ray while animation is being played this works great. My problem is the Input.GetMouseButton method reading on the refrence after a long think i found that you must press the button then release then press again i need it to be just a press none of this press again jargon.
Within my melee system when the user presses the LMB i run a another method which is the melee systems main port of call for detecting hits and what it hit.
Is there anything other than this or am i doing something wrong i am 100% sure i am not i hope there is some sort of solution that someone can help with. here is some code that is within the update method
if(Input.GetMouseButtonUp(0))
{
attacking = true;
animations.animation.Play("Down");
CheckAnims();
}
}
void CheckAnims()
{
if(animations.animation.IsPlaying("Down"))
{
if (Physics.CapsuleCast(point1, point2, radiusOfCapsule, direction, out hit, length))
{
if(hit.collider.gameObject.tag == "Enemy")
{
if(attacking)
{
StartCoroutine(MeleeSystem());
}
}
}
}
}
Answer by john-essy · Apr 12, 2012 at 09:55 PM
I realised i needed a coroutine in the update function whether or not the attacking boolean was true if it was run the code. The reason it was not working was i was running code within the Mouse Event which only returns true for one frame.
Answer by fafase · Apr 12, 2012 at 01:32 PM
GetMouseButtonUp/down means it sends 1 on the frame when you release/press the button.But the rest of the time it sends 0
GetMouseButton sends 1 on every frame you are pressing the button so as long as yuo keep pressing.
Your question is not so clear to me but maybe that is what you need.
I have noticed that when i press the L$$anonymous$$B it will play the animation and any debugs i do as expected. What i have also realised is that when i press the button some times it does not run the checkAnims method but if i press the button twice in quick succession it will run the method. This is the problem i need it to run the method everytime the button is pressed.
As fafase says, you should use Get$$anonymous$$ouseButton() if you want to get true
on every frame the button is held down. The function you're using in the posted sample will only return true on the precise frame where the user released the button.
Also important: using ButtonUp
/Down
from FixedUpdate()
can cause unexpected results. $$anonymous$$ake sure that you're calling this from Update()
specifically.