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.
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’