Game Development Reference
In-Depth Information
2. Add a CharacterController component to the player's GameObject
component as well. If Unity asks you whether you want to replace the box
collider, agree to the change.
3. Create public Vector3 moveDirection that will be used to store the
current actual direction vector of the player. We initialize it to the zero vector
by default as follows:
public Vector3 moveDirection =
Vector3.zero;
4. Create three public float variables: rotateSpeed , moveSpeed , and
speedSmoothing . The first two are coefficients of motion for rotation and
translation, and the third is a factor that influences the smoothing of
moveSpeed . Note that moveSpeed is private because this will only be com-
puted as the result of the smoothing calculation between moveDirection
and targetDirection as shown in the following code:
public Float rotateSpeed;
private float moveSpeed = 0.0f;
public float speedSmoothing = 10.0f;
5. Inside the update loop of this script, we will call a custom method called Up-
dateMovement() . This method will contain the code that actually reads in-
put from the user and moves the player in the game as shown in the following
code:
void Update() {
UpdateMovement()
}
6. Above the update loop, let's implement the UpdateMovement() method as
follows:
void UpdateMovement () {
// to be filled in
}
7. Inside this method, step 1 is accomplished by storing the horizontal projec-
tion of the forward and right vectors of the current camera as follows:
Search WWH ::




Custom Search