Game Development Reference
In-Depth Information
108.
//If it hits the ground,
109.
//then the character is grounded
110.
if(Physics.Raycast(
111.
new Ray(feet.position, -Vector3.up), 0.1f)){
112.
return true;
113.
}
114.
return false;
115.
}
116.
}
Listing 47: Character controller based on physics simulation
We can set the values of jumpHeight and movementSpeed values of the character to match
our needs. Additionally, we have the third variable playerFeet , which must reference an
empty object that is a child of the capsule. This object must be positioned at the bottom
of the capsule, where the feet are supposed to be. Let's begin from the last function in the
script, OnGround() , at line 105. This function tests whether the character is currently stand-
ing on the ground. To perform this task, the function casts a ray using Physics.RayCast()
function. This functions needs a ray and optionally a maximum distance for that ray, and
tells whether this ray has hit something while traveling in its direction. In line 111 we cre-
ate a new ray that starts from the position of the feet and goes downwards. We limit the
travel distance of this ray to 10 cm only, so that it hits the ground only when the character
is actually standing on it. If the ray hits the ground, we return true to indicate that the char-
acter is currently grounded.
Search WWH ::




Custom Search