Game Development Reference
In-Depth Information
Engage thrusters
Now, we will create the script to control our character. Perform the following steps:
1. We will create the
CharacterControl
script that will control our entire menu; go
to
Assets
|
Create
|
Javascript
(for Unity JavaScript users) or go to
Assets
|
Create
|
C#
(for C# user), name it
ChracterControl
, double-click on it to
launch
MonoDevelop
, and we will get our hands dirty with the code.
2. Open the
ChracterControl
script file and type the following variables:
// Unity JavaScript user:
#pragma strict
@script RequireComponent(Animator)
@script RequireComponent(CharacterController)
public enum MOTION_STATE {GROUND,JUMP,FALL,JUMP_HOLD}
private final var GRAVITY : float = 20.0f;
private final var MIN_AIR_TIME : float = 0.15f; //
0.15 sec.
var rotationSpeed : float = 1.0f;
var walkSpeed : float = 2.0f;
var runSpeed : float = 5.0f;
var jumpSpeed : float = 8.0f;
private var _animator : Animator;
private var _hDirection : float;
private var _vDirection : float;
private var _moveDirection : Vector3;
private var _movement : Vector3;
private var _isMoveBack : boolean;
private var _isRun : boolean;
private var _isJumping : boolean;
private var _motionState : MOTION_STATE;
private var _baseCurrentState : AnimatorStateInfo;
private var _characterController : CharacterController;
private var _moveSpeed : float;
private var _verticalSpeed : float;
private var _inAirTime : float;
private var _inAirStartTime : float;