So in our case, the current context is a result of movement. Performed and will therefore have information on our joystick. Code syntax for callbacks in C# is += sex(for context) and then known as a lambda expression, which looks like =>. On the right side of the lambda expression is the lambda body, where we can use our context! Ok, great, so we know that the current context has data on our joystick! How can we access that data? Like our movement Action, the current context has functions built-in and available for us to use! Now, if we don’t already know the type of value the context is holding, we can use the context’s built in “ReadValueAsObject” function and log the value to the console.
Before we test our current logic in play mode, we actually need to enable the ActionMap. We would want to do this when our characters are enabled, so we need to add input.Character.Enable() to an OnEnable function. We can do the reverse with Disable. Entering play mode, we can see it’s a vector2! Great! And if we do the same with the current context of our “run” Action, we can see it’s afloat! Knowing these types, we can store and use them to get our character moving through player input! We will add three new variables at the top of the file: a vector2 titled current movement, and two booleans titled movement pressed andrunPressed.
Locally Stored Values
Back to our callbacks, we can now set your new movement variable equal to context—readValue passing in vector2 as the parameter. However, we want the value to return as true or false for our run callback, whether the button is pressed or not. By default, “readValueAsButton” will return true if the float value is greater than 0 and false when it’s zero! And then for our movement pressed boolean, we need to see if either the x or the value in the current Movement is greater than0! Ok! At this point, we have locally stored values for both of our inputs with the new input system! Let’s get our character moving by going back to our handle movement function. We only want our character to start walking movement pressed is true, but our walking animator parameter is false.
And we want to reverse that logic to stop walking. For both conditions, we’ll use the animator.SetBoolfunction, pass in the isWalking Hash and the appropriate boolean to change the animator parameter value. We can apply similar logic for running, but this time checking to see if both stored player input values, movement pressed and unpressed, are true or false. Before we test our current logic in play mode, we need to invoke handle movement in our update function! Now, let’s test our current progress. Entering play mode and tilting our joystick forward, our character starts walking, pressing the left bumper our character runs and letting go of the stick it will completely stop. Rad! And we can make this even better. Right now, our character only moves in one direction. But we can fix that with the values of our current movement vector2 and a bit of code! Back in our script, we will write a new function called handle rotation. Our current movement variable stores the joystick direction as normalized values between -1 and 1 for both x and y in the vector2.
Grabbing the Character
We can use these values to rotate our character to point in the right direction! We’ll start by grabbing the character’s current position as a Vector3 by using transform. Position. Next, we’ll create a new Vector3 with the two values that we already have stored in the current movement. This is because the world has an x, y, and axis, and that is how our character’s position is stored. We’ll say “Vector3 new position” is equal to new Vector3 passing in the current movement X value for x, skipping y, and using the current movement value for z. And the last Vector3 we need is what we will call the positionToLookAt, the current position and the new position added together. We can now use Unity’s built-in function transform.
LookAt and pass in this combined position! LookAt will rotate the transform so that its forward direction is pointed at the position passed in! And the last thing we need to do is invoke handler rotation in our update function! Pressing play and using our controller, your character will run around, and it will follow the movement of our joystick! We did it! And with a little bit of modification, we can have some isometric gameplay! Great work today! If you want to support the channel, please consider liking the video and subscribing to the channel! It makes a big difference in helping awesome developers like yourself find this channel! In the future, we’ll take this knowledge and apply it to the 360-degree movement! We’ll also start using rigid bodies and character controllers! As always, you’re invited to join our awesome growing channel discord and to follow me on Twitter for channel updates! But that’s all for today. Thanks