Join Jesse Freeman for an in-depth discussion in this video Working with script execution order, part of Advanced Unity 2D: Platformer Player Movement.
- So, at this point our system works really nice. If we look at our player we have separate behaviors for each of the things we want our player to do. We have one the manages the Input State. We have one that manages which direction to face, and we have another for walk. As we continue to grow these behaviors though, we really can't guarantee the order that each of these scripts will execute. One of the things that's a little deceiving in Unity is that even though one script might be above another, in this case the Input State is above Face Direction, it doesn't mean that this is the order that these scripts will actually execute in.
In order to correct this, we're going to have to manually tell Unity which script to execute and in which order. To do this we can go into the Project Settings and go to Script Execution Order. Here, you'll see that we now have the inspector and we have a list that we can create of our scripts. To get started let's add in the Input Manager, the Input State, our Face Direction and our Walk. Also, these are the default times.
But we want to make these a little bit lower. So let's change these each to be 10, 20, 30 and 40. We can also drag and reorder these at any time. Later on, when we add new behaviors we'll also change the order of them as well. Before moving on, we need to hit Apply in order to save these changes. Now, we'll rerun the game and the changes won't be perceivable. Everything should work exactly how we had it before. We can still walk left and right.
We can still run. And our animation still speeds up when we run. The only difference now is that we're going to make sure that the Input Manager samples input before it sets those values on the Input State. And we want to make sure that the Input State calculates everything that it needs to know before Face Direction or Walk look at it. And the most important thing is that Face Direction actually sets the direction on the Input State before Walk looks for that direction.
Here, I noticed, that this didn't get changed. So, we'll just go back and hit Apply and make sure that this reads 10, 20, 30 and 40. Next, we're going to come up with a better system for handling collisions and letting the rest of the behaviors know when a collision has actually taken place.
- Building an input manager for keyboard and controllers
- Setting up the player
- Building reusable, self contained, and modular behaviors scripts
- Adding behaviors: walking, running, and jumping
- Managing animation states and player collisions
- Adding a ducking animation
- Detecting wall collisions
- Sticking to, sliding down, and jumping off walls
- Picking up items
- Firing projectiles