Basic Scripting Set Up

Gerald Clark
5 min readMar 23

--

In this article I’ll cover a basic set up when scripting the functionality for the actions we set up in the previous article.

My goal is to simple print a message to the console when the correct key bindings have been pressed. Lets start with the Fire action.

First, I need to set up a Player script and attach it to my cube. Once thats all set up, there are 3 steps to take when setting this up. First, you need a reference to the input action class created earlier. Mine is called PlayerInputActions. After that, it needs to be initialized in Start(). Then the correct action map needs to be enabled. Since we can have more than one action map, we need to tell Unity when to enable and disable different action maps so we don’t get any weird, unexpected behaviors.

Now if we take a look at the documentation, you’ll see there are a few options for registering the actions. There is a started, performed, and cancelled function for each action. For now, I want to use the performed function. to do this, I simply have to type _inputAction.Player.Fire.performed. From there, I need to register this to a funciton. If you type += then tab it will auto complete the registration and create a function called Fire_performed.

I am simply going to print “Fire!” in the console when I press the space key now.

So now, if I wanted to instantiate a laser beam or something, I could just put the logic for that inside this function. This is really nice because this removes the need for an update function constantly checking if the space key was pressed.

I’m going to register the rest of the actions and create the appropriate functions.

Next, the self destruct method. I’ll just print “Self Destruct!” when I press the appropriate keys. In my case holding F, then pressing D will call this function.

We can repeat this for all the functions. I want to briefly go over how I can actually get the cube moving in this next example. Instead of printing something to the console.

WALK LOGIC

Before I get it moving I want to explain this InputAction.CallbackContext obj thing. Since our Walk action is a value type action and the control type is a Vector2, I can actually see what key I’m pressing by accessing this obj parameter.

When I put Debug.Log("Walking: " + obj); inside the walk action performed function and press A,D,W, and S it will print this super long message. The important thing here is the value = (x,y) part.

As you can see, A is going to be -1 on the X and 0 on the Y. D is going to be 1 on the X and 0 on the Y. W is 1 on the Y and 0 on the X and S is -1 on the Y and 0 on the X. Its literally printing out the Vector2 values associated with each key press!

So right now I have the ability to know which keys are pressed. How do we MOVE THE CUBE NOW!?

I’m going to create a variable to store the Vector2 value read when pressing the key. then transform.Translate to move the object.

The problem with this is that the object is only being moved everytime the key is pressed. This is because this is a performed method. We could use started and change the action to have a hold interactor and all that OR I can just poll the input readings in update and move the object that way.

Now I’ll be moving smoothly. You could even change how the object moves. Instead of moving on the x and y axis, just like the Input.GetAxis method, I can move the object on the x and z for example. This can be achieved by simply moving the object by a new Vector3 and assigning the x and z values the move Vector2 values. SO it would look like this:

transform.Translate(new Vector3(move.x, 0, move.y);

Finally I’ll get into the sprint logic.

SPRINT LOGIC

As with anything, there are a million ways to achieve the same result. I’ll implement the sprint logic this way to go over the canceled function.

So far I’ve only covered the performed function. I want to use canceled so I can know when I lifted my left shift key up.

So I created a speed variable. I set it to 5, When I perform the Sprint_performed function, my speed will increase by 5 and when I let go it’ll reduce by 5. Since I’m constantly checking the input readings of the walk action and moving by the speed variable, all I need to do is change that value when I press and let go of the left shift key.

You can see the value change in the inspector on the right when I press left shift.

In the next articles I’ll cover some more common uses for certain actions and maybe implement a pretty charge rifle shot or something to chowcase how cool this system can be. See ya there!

--

--

Gerald Clark

Father Game Developer Music Composer Sound Designer