Unity - action while button pressed

Reading time ~1 minute

I usually make games to be released on the Andrid platform and my UI usually involves buttons that have to be held down in order for something to happen. For example, the player character should walk forwards while the move button is held down.

Here is how I achieved this through the event trigger system and public methods on my scripts.

Public methods

Since Unity uses C# as it’s scripting language methods can be marked as public or private. In order for something outside of the script to call a method we have to mark the method as public. We do this like below;

public void DoSomething()
{
}

Using the event trigger system

The way I’ve been moving my sprites in my games may be to move the position of the transform of the sprite by using methods should as MoveTowards. Such methods will want to be called continuously as the button is pressed. In order to achieve this I create a private boolean field in the script, such as;

bool moveForward;

Then in the FixedUpdate method of the script you can check if the bool is true and if it move your player as desired.

void FixedUpdate()
{
  if(moveForward)
  {
    // move player as desired
  }
}

In order to set the moveForward field to true I use the event trigger system to call a public method on the script. So for the Pointer Down event on the Event Trigger component, within the button game object, call;

public void MoveButtonPressed()
{
  moveForward = true;
}

and for the Pointer Up event, call the method to set the boolean to false;

public void MoveButtonReleased()
{
  moveForward = false;
}
event trigger
Button event trigger

Adding the instance of the script

The final point is just a reminder to myself since I often forget.

You need to add an instance of a script that you want to call and not the script itself. For example, you add the game object to the event trigger that your script is attached to, not just the script.

Coverage Gutters - VS Code

Use the Coverage Gutters extension in VS Code to understand your test coverage even better Continue reading

XP Manchester - Why isn't XP the norm?

Published on February 22, 2021

Metrics give you the bad news

Published on January 23, 2021