Game Development Reference
In-Depth Information
10. We calculate the move direcion here by using Vector3.Slerp() , and normalize
it, because we only need the direcion where our character moves from the user
input, as shown in the following script:
//If the target direction is not zero - that means there is no
button pressing
if (v3_targetDirection != Vector3.zero) {
//Rotate toward the target direction
v3_moveDirection = Vector3.Slerp(v3_moveDirection, v3_
targetDirection, f_rotateSpeed * Time.deltaTime);
v3_moveDirection = v3_moveDirection.normalized; //Get only
direction by normalizing our target vector
} else {
v3_moveDirection = Vector3.zero;
}
Vector3.Slerp() is the funcion that we can use to interpolate between
two vectors spherically by amount of ime, and the return vector's magnitude
will be the difference between the magnitudes of the first vector and the
second vector. This funcion is usually used when we want to get the smooth
rotaion from one vector to another vector in a ixed amount of ime. You can
see more details at the following Unity website:
http://unity3d.com/support/documentation/
ScriptReference/Vector3.Slerp.html .
11. We get the moving speed of our character by checking if our character is walking or
running. In this secion, we also check whether the character is grounded or not. We
will make sure that we cannot press the Run or Jump butons while the character is
in the air:
//Checking if character is on the ground
if (!b_isJumping) {
//Holding Shift to run
if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.
RightShift)) {
b_isRun = true;
f_moveSpeed = runSpeed;
} else {
b_isRun = false;
f_moveSpeed = speed;
}
//Press Space to Jump
if (Input.GetButton ("Jump")) {
f_verticalSpeed = jumpSpeed;
b_isJumping = true;
}
}
 
Search WWH ::




Custom Search