- Home /
The question is answered, right answer was accepted
Input.GetMouseButton(0) registering twice on relase?
I am using Input.GetMouseButton(0) to add force to my character. The problem is that it seems to register twice when I release the mouse button. Why is this happening? this is the code I am using:
if (Input.GetMouseButton(0))
{
controller.velocity = Vector2.zero;
controller.AddForce(new Vector2(0, 75f));
}
I am not using any mouse or touch controls anywhere else in my game.
Any help is very much appreciated.
Answer by pako · Oct 31, 2017 at 10:27 AM
if (Input.GetMouseButton(0))
{
if(!GetMouseButtonUp(0)){
controller.velocity = Vector2.zero;
controller.AddForce(new Vector2(0, 75f));
}
}
Input.Get$$anonymous$$ouseButtonDown(0)
will return true once. I want to add the force as long as the mouse button is held. $$anonymous$$y problem is that it adds the force twice once I release the mouse button. So it looks like its adding 150f to the velocity.
I'm not entirely sure why you're getting the issue you're getting, you should understand that 'Input.Get$$anonymous$$ouseButton(0)' will return true each frame that the mouse button is held down (assu$$anonymous$$g it's inside the update function), so if you were to click the mouse button, it would most likely register it atleast twice, since you had the mouse clicked for more than 1 frame. You say you want to add velocity for as long as the mouse button is held down, how can you tell that when you release the mouse it does it twice if this is executing every frame?
Hello @AidanHorton and thanks for your reply. As I said, I am actually getting the affect that I want by using Input.Get$$anonymous$$ouseButton(0)
. The problem that I am facing occurs only when I release the mouse button. And answering your question, the amount of force applied to the Y axis seems okay as long as the mouse button is held down, But once I release the mouse button, the added force applied seems to be twice as much (the distance that it travels is bigger). So in the end, I am only judging visually.
@tmalhassan how do you check that you have released the mouse, and that after that release the force is added twice?
Because if you can check the mouse release accurately, you can then -maybe- add the force only "if the mouse has not been released".
I am not checking it accurately, I am only judging visually. I don't think I can check it accurately though (since I am calling it every frame). Or is there a way to check it accurately?
I just revised my answer to make sure that after the mouse button is released, force does not get added any more.
Your new answer solved the issue. Although, I still don't get why it would behave like this using Input.Get$$anonymous$$ouseButton(0)
. Thank you :)
I'm not sure why this happens, but I suspect that Input.Get$$anonymous$$ouseButton(0)
is true for one more frame after you release the button.
You could probably verify that if you try:
if (Input.Get$$anonymous$$ouseButton(0))
{
if(!Get$$anonymous$$ouseButtonUp(0)){
Debug.Log("Button Released - Total Frames: " + Time.frameCount) //true for just 1 frame
}
Debug.Log("Total Frames: " + Time.frameCount)
controller.velocity = Vector2.zero;
controller.AddForce(new Vector2(0, 75f));
}
Answer by jchester07 · Oct 31, 2017 at 11:30 AM
Obviously, it will "Add Force" to the existing velocity of your controller.
If you want to maintain a constant velocity, then just set the velocity directly instead of setting it to zero then adding force.
The reason why I am setting it to the velocity to 0, is because it would give it the affect of falling down once the mouse button is released. The way velocity works is that it will keep redusing till it reaches 0 (This will give the rigidbody a floating kind of effect). Unlike when you set it to 0, it would stop and immediately start falling down. Thanks a lot for your answer.
Well, I think logically it would still result in the same effect.
But since it's better to use AddForce than setting the velocity directly as also advised by Unity, then your code is good to go. And by using AddFoce you can set different force modes which is better.
Answer by AidanHorton · Oct 31, 2017 at 11:38 AM
You are using AddForce when you could simply set the velocity. Try something like this, it should be more lightweight as well (but it may not exactly fit your use case):
void Update()
{
if (Input.GetMouseButtonDown(0))
{
controller.velocity = new Vector2(0, 75f);
}
}
What it does, is when you press the mouse button down, it sets the velocity to be 75f. It will stay like that until some external interaction of some kind. If you want to ensure it stays at 75f for the entire time you have your mouse button down, then simply change 'Input.GetMouseButtonDown' to 'Input.GetMouseButton'
The problem with Input.Get$$anonymous$$ouseButtonDown(0)
is that it will only add the force once until I release the mouse button and then click it again (Which isn't really the affect that I want). Regardless, thank you for your answer :)
Forgive me, I don't think I still fully understand the issue, but I mentioned you could change it to 'Input.Get$$anonymous$$ouseButton' if you want it to constantly update, this would set the velocity to 75f while you have your mouse button held down. What you were doing before was a really convoluted way of setting the velocity, where you set the velocity to zero, then added a certain amount of force. Using controller.velocity sets the velocity, and it means there shouldn't be an issue with too much force being added, since you aren't adding force, simply setting it. If there is still an issue after that it must be an external issue elsewhere in the code where you add velocity.
Answer by znickbar · Apr 29, 2020 at 07:10 PM
@tmalhassan You may try Input.ResetInputAxes(); it works for my scenario.