Game Development Reference
In-Depth Information
motor.movement.maxForwardSpeed = int_moveSpeed;
motor.movement.maxSidewaysSpeed = int_moveSpeed;
motor.movement.maxBackwardsSpeed = int_moveSpeed;
////////////////////////////////////////////////////////
//Checking if the character is moving or not
if (directionVector != Vector3.zero) {
if (int_moveSpeed == walkSpeed) {
_animation.CrossFade(walkAnimation.name);
} else {
_animation.CrossFade(runAnimation.name);
}
} else {
_animation.CrossFade(idleAnimation.name);
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation *
directionVector;
motor.inputJump = Input.GetButton("Jump");
}
}
In the preceding funcion, we have used
Input.GetKey(KeyCode.E)
for aiming and
Input.GetKey(KeyCode.LeftShift) || Input.
GetKey(KeyCode.RightShift)
for running, which will get the input
as the
E
key and right arrow/left arrow keys on the keyboard. However, this
isn't flexible if we want to change the input or if we want to put this game on
another plaform that doesn't have a keyboard. We can solve this by seing the
custom
Input
buton via the
Input Manager
and using
Input.GetButton()
instead of
Input.GetKey()
, which is much more dynamic for adjusing the
input controller for diferent plaforms. We can go to the
Input Manager
(
Edit
|
Project Settings
|
Input
), which we have already menioned in the irst chapter.
4. Go back to Unity, click on the
First Person Controller
object in the
Hierarchy
view,
go to its
Inspector
view, and right-click the
FPSInput Controller (Script)
and choose
Remove Component
to remove it. Then, we will go to the
Project
view and drag
the
New3PSController
script that we just created to the
First Person Controller
object in the
Hierarchy
view.
5. Go to the
Inspector
view of
First Person Controller
object in the
New3PSController
component and set the following:
Animation
:
robot
(
robot
game object in the
Hierarchy
view)
Idle Animation
:
idle
(
Chapter5/Robot Artwork/robot@idle/idle
)
Walk Animation
:
walk
(
Chapter5/Robot Artwork/robot@walk/walk
)
Run Animation
:
run
(
Chapter5/Robot Artwork/robot@run/run
)