- One: Control ped velocity property
- Two: Control ped current vehicle speed
- Three: Control an object velocity, and attach the ped to this object
To control the direction of the flight we can use game camera direction or create and vector3 to control the direction using keys like we can do with an plane or helicopter.
Download the code of this tutorial here
First ped flight idea (Basic one)
Method one is very simple but has some limitations, all we need to do is control the ped velocity, if we set the ped velocity is x=0, y=0 and z=1 we will make it float because he will be frozen in X and Y and will be with 1 in Z, and considering the common 3D vector components X and Y means horizontal measure and Z vertical:
The limitations of this idea, that i noticed, are:
- Speed, we can't reach more than 80 (mph i guess)
- Can't control Pitch and Roll, we can't change the pitch or roll of the ped so we will always have the animation pitch or a hard to control pitch in result of ragdoll state, we only can set the heading
Controlling the flight
To generate an controllable flight we can:
- Change directly his velocity to the vector velocity that we want, for example, we can make him move in the camera direction setting the ped velocity to be equal the camera direction, also we can use an incremental multiplier to determine the speed multiplying the camera direction by the multiplier.
As we can see we have some side effects, for example, ped can turn into Ragdoll state and then we are vulnerable to collisions and we can accidentally trigger that automated ground camera that is used to look at ped when he is falling.
- Or we can set ped velocity to always zero (vector3.zero - x, y and z = 0) and use ApplyForce to push the ped to where we want, controlling his flight:
It's more easy use the second idea.
Second ped flight idea (using invisible vehicle)
This ped flight idea i saw for first time in Superman script made by nixolas1, i used same concept in my Simple Car Fly cheat with no good results because cars have some kind of turn mass velocity (or inertia) that affects the rotation of the car when hit something.
In resume the idea consists in warp or attach the ped to an invisible vehicle, then control vehicle velocity and direction. I use an small bike (Faggio) because bikes have an better result with this speed control idea and the "collision area" is smaller :)
So we need an vehicle:
We need to create this new vehicle and hide it:
And, when activating flight, we need to attach or warp the ped into this vehicle:
Ok, now we have our invisible bike:
Let's make it fly, in the tick we need to change this bike direction based on the flight direction and control his speed to make it float.
We can do this setting bike speed to zero and using ApplyForce to push the bike in the flight direction, but we will have the max speed of 80 again. To reach higher speeds we need to use an native call:
Using this native call we always will move the bike in his direction with the speed determined by the native call:
Native.Function.Call("set_car_forward_speed", myFlyingVehicle, force)
One side effect of this idea is that with speeds over 200 we have some instability, sometimes speed just drops back to 80 :(
Result (Obs.: In this vid i'm already using the flight animations):
I'm not sure why i can't make it fly with speeds higher than 80 in this script sample, in Iron Man IV i use same idea and can reach more than 80.
Third ped flight idea
An third flight method is use an object and the native call SET_OBJECT_INITIAL_VELOCITY:
Also we need to attach the ped to this object and detach when finish the flight:
The flight animations
To make the impression of flight more real we can use some EFLC parachute animations like:
- full_brake_for_landing - To simulate an float state with low speed
- free_fall_deccelerate - To simulate an medium flight speed
- free_fall_fast - To simulate an flight at full speed
To use this in GTA IV we need to export the parachute.wad from EFLC anims.img and import into GTA IV.
To make the ped play the animation we use this:
And to stop the animation:
I will talk more about animations in a future tutorial :)