Richard Morgan

There are many ways to handle player management. Here is what I would consider the preferred way…

This is an example of a clean player management setup:

The idea is that the Input Actions Script handles the inputs, the Player Management Script handles the processes triggered by the inputs, and the Player Script performs the actions of the processes on the Game Object.

In this case, we want a simple movement script. Here’s the Input Action panel:

--

--

Let’s create an action map for our dog. Here are the inputs:
Bark Action (Space key)
Walk Action (WASD keys)
Run Action (Left Shift held down for more than 1 second)
Die Action (D + F keys at the same time)

The Bark Action is a button:

And it’s bound…

--

--

Here’s a simple example that shows how Action can replace delegate/event statements and accomplish the exact same thing.

Here we have a UI script that needs to be updated whenever the Damage method of the Player script is executed.

We declare our Action in the Player and set it to static so the other scripts can access it directly. In our UI script, we add the UpdateHealth method to the new Action. Then, back on the Player script, we trigger the Action when the Damage method is executed.

This separates our scripts so that one script does not have to know the other exists. The scripts can operate independently and now the scripts are reusable.

--

--

Here’s a quick way to respawn in Unity using delegates and events…

In this example, we’re going to use the SpaceBar to initiate the respawn event:

Inside our Main script (attached to the MainCamera), we’ll create the Teleport delegate and give it a Vector 3 value that will be the destination. And also create an event that is tied to the delegate, and make a variable to hold our destination as a Vector 3 value:

--

--

Richard Morgan

Richard Morgan

Unity and Flutter developer interested in creating mixed-reality applications.