Flight mechanic

=The Flight Compendium= In this tutorial I'll cover all of the common ways you can use flight in your game, including a number of different game mechanics. If there are flight mechanics you want in your game that aren't covered here, just tell me (Invisibledoogey) in the site's chatbox and I can add them.

FLIGHT BASICS
Y6Nc5eThis is the most basic kode you can use to have your character fly. Let’s talk about the default flight mechanics. Using this kode, your character is in a perpetual state of “flight,” even when they touch the ground. If you look up with your camera, your character flies higher. If you look down with your camera your character flies lower. This line of kode will be the basis that we build on for all other sections that follow (except the airplane section at the end).

HOVER FLIGHT
7OGBUSIf you want your character to hover at a certain height above the ground instead of fly, you would use this kode. What line 1 is doing is using a ray cast hit downward (which looks for when an invisible line drawn from you downwards hits an object) and determining the coordinates at which that ray cast hits the ground. That is where the [hit position] tile comes into use.

Line 2 then sets your player’s Y position to be the same as the position at which your ray cast hits the ground, PLUS two spaces up. You can modify that [2] offset at the end to make your player hovers as high above the ground as you want.

Edit: Took me ages to figure this out, the explanation here helped me a tad tho for the most part i had to completely teach my self raycasting to understand it and how difficult it is put together from this without images, so i figured it out here's what it looks like.

First line: WHEN: Raycast hit, direction, down, terrain only

2nd line: DO: Position, Y, =, hit position, Y, +, 1 (or how ever many spaces you want)

CONTROLLED FLIGHT
The default flight controls make flying high/lower quite hard, forcing you to dip the camera up and down. So let’s instead use button inputs to move you up and down in the world. This can simply be koded the following way. This is also in the default “3rd Person Hover Flight” in the Brain Gallery, which you may want to check out. FMlNwgThis kode moves you up and down further in the world as you hold down on the button input.

CONTROLLED HOVER FLIGHT
Now let’s combine the last two pieces of kode into one. Say you wanted your player to only fly at a certain height (moving up or down on the camera doesn’t change their flight height), but you could increase or decrease that height with the press of a button.

To accomplish this, we need to throw in a number variable on top of the previous vector variable and have the X/Y buttons change that number variable instead of moving world up and down.

Here is what the combined kode would look like: 9aKyJjThat last child line underneath the X is important, so that your character will never fly lower than the ground. So make sure to include that!

ACTIVATING FLIGHT POWERS
One common request is to have a power or button press “activate” flight mode. This can simply be accomplished with a page switch, since flying and non-flying controls do not play nice when put on the same page.

In this example, let’s say you want to have a double jump activate flight. When you jump, if you hit the jump button again, you start flying. I’m using this as an example because it’s also a useful way to show you how to activate abilities when double-tapping a button.

This is what activating the flight power on double-tapping the Jump button looks like. 99LjmV*Lines 2-4 are toggling the Boolean “First Jump” true and false. Now that you’ve switched to your flight page, we need to give it a condition to turn off and go back to the first page. Otherwise, once you enter flight mode, you stay in flight mode the entire rest of your game.
 * Line 2 checks to see if the “First Jump” Boolean is true. If it is true, it toggles it false and switches to the flight page on line 3.
 * Line 4 sees that if the “First Jump” Boolean is false, then it toggles it true. This is what allows nothing to happen the first time you hit A, but something happens the second time, since the boolean has now been made true.
 * Line 5-6 also state that if “First Jump” is true and nothing happens in 15 frames (which equals half a second, due to 30 frames per second), then it turns it to false. Without this, after you hit A the first time, you could hit it a second time 30 seconds later and you would still enter flight mode.

In this example, we’ll say flight mode is a timed event. And after 5 seconds, you stop being able to fly. Here is the kode to accomplish that. EjBy3T*Line 1-2 wrap your flight mechanic into a duration timer. So after 5 seconds, you can’t fly around anymore.
 * Line 2, you can replace or add to with whatever extra bits of flight mechanics we covered earlier.
 * Line 3 is what makes you fall back to the earth. Without this, you will stay hovering in midair.
 * Line 4 detects when you bump up against terrain or an object, you go back to your walking page (side note: make sure you are naming your pages).

AIRPLANE FLIGHT
Now let’s talk about pitch, yaw and roll. Say you’re making a game with an airplane and you want to DO A BARREL ROLL!!!! Lines 3 and 4 of the below kode allow you to do that. With right bumper/left bumper, your plane will roll to the left or right. The [invert] kode is what makes it roll the other way. BsaqJ9Now say you want to have realistic airplane movement as you are flying. The above kode throws our previous examples out the window.
 * Line 1 and 2 set that the plane is always flying and when it is flying, it does certain things. These certain things specifically are lines 3 to 6.
 * We already covered lines 3-4. They allow you to DO A BARREL ROLL!!!!
 * Lines 5 and 6 are set up this way so that your plane can rotate on its vectors simultaneously. You want to do this instead of koding specific actions for left stick down, up, left, right. Because by koding those as left stick directions, you are limiting your plane movements because each left stick movement line can’t run at the same time. But vectors can.
 * The [invert] on line 5 will invert movement on your Y axis. Which if you’ve played a lot of flight simulator games, you’ll understand why you’d want the inversion. Of course, this is just personal preference.

That’s it for the flight compendium for now. Again, if you want to request anything else added here, drop a line in the site’s chatbox.