Game Development Reference
In-Depth Information
12. We apply the gravity and calculate in-air iming of our character. We need to apply
the gravity here because the Move() funcion in the character controller script
doesn't have any gravity applied to it. We will use the in-air ime to track the ime
when the character is in the air. This will make sure that our character can walk
down on the slope without any bugs:
// Apply gravity
if (IsGrounded()) {
f_verticalSpeed = 0.0; //if our character is grounded
b_isJumping = false; //Checking if our character is in the air
or not
f_inAirTime = 0.0;
f_inAirStartTime = Time.time;
} else {
f_verticalSpeed -= gravity * Time.deltaTime; //if our
character in the air
//Count Time
f_inAirTime = Time.time - f_inAirStartTime;
}
13. We calculate the movement of our character and use the Move() funcion to move
our character:
// Calculate actual motion
var v3_movement : Vector3 = (v3_moveDirection * f_moveSpeed) +
Vector3 (0, f_verticalSpeed, 0); // Apply the vertical speed if
character fall down
v3_movement *= Time.deltaTime;
// Move the controller
c_collisionFlags = controller.Move(v3_movement);
14. Finally, we apply rotaion to our character when the user controls our character let
or right:
//Update rotation of the character
if (v3_moveDirection != Vector3.zero) {
transform.rotation = Quaternion.LookRotation(v3_
moveDirection);
}
}
Next, we will assign this script to our character by going back to Unity and dragging the
CharacterControl script to the Heroine_animate in the Hierarchy view; we will be able
to control our character, but there will be no animaion applied to our character yet. We will
apply animaion to our character/character animaion in the next secion.
 
Search WWH ::




Custom Search