Game Development Reference
In-Depth Information
If controlling the character is allowed, we have to clear its current velocity on x and z axes,
and then apply the new velocity. Steps in lines 62 through 80 are similar to their counter-
parts in the previously written FirstPersonControl script (Listing 9). The difference is in the
implementation of the movement, since we implement it this time by setting the velocity
of the rigid body on x and z axes in accordance to the control flags. In line 83, we store the
newly computed velocity back into rigidbody.velocity , which makes the physics simulator
move the object based on this new velocity. Keep in mind that all these steps did not touch
the y value of the velocity, and hence have no effect on the jumping or falling state of the
character.
The next step at line 86 is reading jump input and applying jump if the character is grounded.
Jumping is implemented by applying a one-time force (pulse) to the character. This force must
push the character upwards in the air, until it reaches the specified jumpHeight and then starts
to fall down. From a physical point of view, our character is a projectile that is going to be
thrown in the air with an initial speed. As the time goes, this speed is going to be reduced
until it reaches zero at the maximum height. After that, the projectile begins to fall down
again by the force of gravity. All we have to do is to compute the correct initial velocity for
the projectile, which depends its mass. This computation requires us to dive into physics and
remember some laws of projectiles. The following paragraph discusses in details how can we
calculate the force magnitude in order to reach the desired jump height. If you do not like
physics, you are free to skip it.
2
2
We use the projectile formula, v 2 -v 1 =2 as , where v
is the velocity of the objects when it reaches its
maximum height, v l is the initial velocity of the object when it leaves the ground, a is the
acceleration, and s is the maximum height the object reaches. In our case, v 1 is the sole un-
known we need to work out for. At the maximum height, the velocity of the object reaches
zero before it starts to fall, hence v 2 =0 . s in our case is the value of jumpHeight , which
is also known to us. As the object goes up, it loses its velocity due to the acceleration o f its
w eight, which is the acceleration of the gravity ( 9.8 m/ s 2 ). By working out for v l , we
get v l = -J -2 as , which is expressed in lines 91 and 92 in the script. Since jump
Search WWH ::




Custom Search