Efficient Jumping Behavior ( and Tutorial )
![immor7al](http://forums.gamesalad.com/applications/dashboard/design/images/defaulticon.png)
I recently made a demo, which I published under the title "Efficient Jump" to the web. Unfortunately, I've not been able to get it to load. I'm not sure why that is.
However, it is in the Game Salad Creator if you just search for "Efficient Jump".
What's so efficient? Last time I dabbled with Game Salad was around this time last year. One of the many ways to do jumping back then was to have two platforms, one that allowed the player to jump and one that was there mainly for collision.
This 'double platform' method has several inefficiencies. It focuses heavily on perfect positioning of the platforms. It also requires far more Actor-scope variables than is necessary. This overcomplicates the solution.
When I wanted to make a game that required jumping, I took a step back. I reevaluated what each preset attribute does and started trying things out. I came up with this simple solution that works like a charm.
First, we need to setup the scene's and actor's physics so that it's a pristine Side-Scroller environment.
Let's go to our Scene Attributes for our 'Initial Scene'. What's important here is Gravity in the Y direction. Set that to some large value, like 1200.
Now, we need to make our actors. Go to Home and Actors tab. Under Actor Tags, click the [ + ] twice. We will name our tags "platform" and "players". Let's make two Actors. Click the [ + ] under Actors. We will name our Actors "player" and "block". Now, we will add these Actors to their respective Tags.
Left-mouse-button drag the Actor "player" into the Actor Tag "players". Do the same action for the Actor "block" into the Actor Tag "platform".
Let's make the Behaviors to get this thing working. First, open up the Actor "block". We don't want this to move at all or make our "player" bouncy. So, open up the Physics part of the Attributes. Set "Restitution" to 0 and set "Movable" to an empty box.
We can also put in the Collide behavior here or we can put it in our "player". Let's do the latter. Click on Home, Actor Tab, and open up the "player". Open up the Physics and set "Restitution" to 0. Set "Fixed Rotation" to a checked box. Set "Drag" to 400. We also need to add another attribute to this Actor to define whether or not it is in the air ( or jumping ). So, let's click the [ + ] under Attributes, select a Boolean type, and name it "inAir". Time to set the Rules and Behaviors.
Let's first add the "Collide" Behavior. Set it to "actor with tag" and "platform". This will ensure that if we ever make any more Actors and put them in the Actor Tag "platform", they will collide with "player". Let's also add the "Control Camera" Behavior. Now, let's add some "Rule" Behaviors.
Our first Rule: We want 2 conditions and set the Rule to "When ( All ) have to be valid". Our first condition is Actor receives event ( key ) ( Left Arrow ) key is ( down ). The second condition is an Attribute. In the Attribute Browser, we want player -> Motion -> Linear -> X. Set the sign to greater than... ( > ). Set the value to -200. This will make it so that our X velocity cannot go under -200 ( sort of like Max Speed ).
The reason why I don't use Max Speed is because it is the Max Speed of Angular Velocity. This means that the combination of X and Y velocity can't be over 200. That's no good. What if I don't care about how fast the player is going in the Y direction? Giving our Rules this extra condition achieves this goal.
So, we need a Behavior to put in our Rule. Let's put in Accelerate, because I don't like the crispness of Change Velocity. Since our Rule is activated by pressing the Left key, type in 180 for the Accelerate's Direction. This should be Relative to: Scene. Let's set Acceleration to something like 1200.
Copy and paste this last Rule for when we move to the Right. Change the greater than sign to a less than sign
( < ) and the value afterwards to 200. Set the Accelerate's Direction to 0. Done!
Now, for our Jump Rule! Let's copy and paste our previous Rule again. Change the key to ( Space ). Change Attribute condition by looking in the Attribute Browser for player -> inAir. Set the condition ( is false ).
Delete the Accelerate Behavior in this Rule. We're going to add a Change Attribute Behavior, instead. Why not any of the other Behaviors? Well, the short answer is I've tried them all and Change Attribute provides the most fluid motion for jumping. So, let's find our Attribute we're going to change. In the Attribute Browser, choose player -> Motion -> Linear -> Y . The value we set it to will be something you will have to fine tune based on your game. I set mine to 575. Done!
Our final rule, YES! Let's copy and paste our previous rule. This final rule will define when the player is inAir or not. Before I go on, just think about it. How do we determine if something is in the air in the real world?
We know that gravity is always acting on something that is flying, so there must be a force acting against gravity to keep something in the air. When we're on the ground, that force acting against gravity is the Earth. In the air, it is something else. How can we measure this in Game Salad? Well, it's the Actor's Y velocity and/or Angular Velocity. This is what determines if the player is moving through the air, so to speak, and not on the ground.
So in our final rule, change the Rule so that "When ( Any ) conditions are valid". Also, change the conditions so both are Attributes. Set the first attribute to player -> Motion -> Linear -> Y. Set the second attribute to player -> Motion -> Angular Velocity. We want both of these set to =/= 0 ( not equal to zero ). If either of these are not 0, then the player must be moving in the Y direction and moving in the air. So we want a "Change Attribute" behavior in the Rule. Set the attribute to player -> inAir to True.
Now we need to add a Constrain Attribute Behavior in the Otherwise portion of this rule. Set the attribute to player -> inAir to False.
Done!
Add blocks and players into your scene, resize them as needed. I'd recommend put a border around your scene so that your player doesn't fall out of the scene.
Cheers
However, it is in the Game Salad Creator if you just search for "Efficient Jump".
What's so efficient? Last time I dabbled with Game Salad was around this time last year. One of the many ways to do jumping back then was to have two platforms, one that allowed the player to jump and one that was there mainly for collision.
This 'double platform' method has several inefficiencies. It focuses heavily on perfect positioning of the platforms. It also requires far more Actor-scope variables than is necessary. This overcomplicates the solution.
When I wanted to make a game that required jumping, I took a step back. I reevaluated what each preset attribute does and started trying things out. I came up with this simple solution that works like a charm.
First, we need to setup the scene's and actor's physics so that it's a pristine Side-Scroller environment.
Let's go to our Scene Attributes for our 'Initial Scene'. What's important here is Gravity in the Y direction. Set that to some large value, like 1200.
Now, we need to make our actors. Go to Home and Actors tab. Under Actor Tags, click the [ + ] twice. We will name our tags "platform" and "players". Let's make two Actors. Click the [ + ] under Actors. We will name our Actors "player" and "block". Now, we will add these Actors to their respective Tags.
Left-mouse-button drag the Actor "player" into the Actor Tag "players". Do the same action for the Actor "block" into the Actor Tag "platform".
Let's make the Behaviors to get this thing working. First, open up the Actor "block". We don't want this to move at all or make our "player" bouncy. So, open up the Physics part of the Attributes. Set "Restitution" to 0 and set "Movable" to an empty box.
We can also put in the Collide behavior here or we can put it in our "player". Let's do the latter. Click on Home, Actor Tab, and open up the "player". Open up the Physics and set "Restitution" to 0. Set "Fixed Rotation" to a checked box. Set "Drag" to 400. We also need to add another attribute to this Actor to define whether or not it is in the air ( or jumping ). So, let's click the [ + ] under Attributes, select a Boolean type, and name it "inAir". Time to set the Rules and Behaviors.
Let's first add the "Collide" Behavior. Set it to "actor with tag" and "platform". This will ensure that if we ever make any more Actors and put them in the Actor Tag "platform", they will collide with "player". Let's also add the "Control Camera" Behavior. Now, let's add some "Rule" Behaviors.
Our first Rule: We want 2 conditions and set the Rule to "When ( All ) have to be valid". Our first condition is Actor receives event ( key ) ( Left Arrow ) key is ( down ). The second condition is an Attribute. In the Attribute Browser, we want player -> Motion -> Linear -> X. Set the sign to greater than... ( > ). Set the value to -200. This will make it so that our X velocity cannot go under -200 ( sort of like Max Speed ).
The reason why I don't use Max Speed is because it is the Max Speed of Angular Velocity. This means that the combination of X and Y velocity can't be over 200. That's no good. What if I don't care about how fast the player is going in the Y direction? Giving our Rules this extra condition achieves this goal.
So, we need a Behavior to put in our Rule. Let's put in Accelerate, because I don't like the crispness of Change Velocity. Since our Rule is activated by pressing the Left key, type in 180 for the Accelerate's Direction. This should be Relative to: Scene. Let's set Acceleration to something like 1200.
Copy and paste this last Rule for when we move to the Right. Change the greater than sign to a less than sign
( < ) and the value afterwards to 200. Set the Accelerate's Direction to 0. Done!
Now, for our Jump Rule! Let's copy and paste our previous Rule again. Change the key to ( Space ). Change Attribute condition by looking in the Attribute Browser for player -> inAir. Set the condition ( is false ).
Delete the Accelerate Behavior in this Rule. We're going to add a Change Attribute Behavior, instead. Why not any of the other Behaviors? Well, the short answer is I've tried them all and Change Attribute provides the most fluid motion for jumping. So, let's find our Attribute we're going to change. In the Attribute Browser, choose player -> Motion -> Linear -> Y . The value we set it to will be something you will have to fine tune based on your game. I set mine to 575. Done!
Our final rule, YES! Let's copy and paste our previous rule. This final rule will define when the player is inAir or not. Before I go on, just think about it. How do we determine if something is in the air in the real world?
We know that gravity is always acting on something that is flying, so there must be a force acting against gravity to keep something in the air. When we're on the ground, that force acting against gravity is the Earth. In the air, it is something else. How can we measure this in Game Salad? Well, it's the Actor's Y velocity and/or Angular Velocity. This is what determines if the player is moving through the air, so to speak, and not on the ground.
So in our final rule, change the Rule so that "When ( Any ) conditions are valid". Also, change the conditions so both are Attributes. Set the first attribute to player -> Motion -> Linear -> Y. Set the second attribute to player -> Motion -> Angular Velocity. We want both of these set to =/= 0 ( not equal to zero ). If either of these are not 0, then the player must be moving in the Y direction and moving in the air. So we want a "Change Attribute" behavior in the Rule. Set the attribute to player -> inAir to True.
Now we need to add a Constrain Attribute Behavior in the Otherwise portion of this rule. Set the attribute to player -> inAir to False.
Done!
Add blocks and players into your scene, resize them as needed. I'd recommend put a border around your scene so that your player doesn't fall out of the scene.
Cheers
Comments