Last active
October 14, 2016 15:42
-
-
Save ThinhHB/9146abb460f729a02e07e513e96759ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public MainPlayerController : MonoBehavior | |
{ | |
CharacterState _currentState; | |
void Start() | |
{ | |
_currentState = CharacterState.Walk; | |
} | |
void Update() | |
{ | |
switch (_currentState) | |
{ | |
case CharacterState.WALK: | |
UpdateWalkState(); | |
case CharacterState.JUMP: | |
UpdateJumpState(); | |
case CharacterState.DEAD: | |
UpdateDeadState(); | |
} | |
} | |
void UpdateWalkState() | |
{ | |
// checking input to move character | |
if (Input.GetKey(Keycode.A)) {//...} | |
if (Input.GetKey(Keycode.D)) {//...} | |
// change state | |
if (Input.GetKeyDown(Keycode.Space)) | |
{ | |
// add force to rigidbody | |
rigidbody.AddForce(...); | |
// change state | |
_currentState = CharacterState.JUMP; | |
} | |
} | |
void UpdateJumpState() | |
{ | |
// checking onGround | |
if (IsOnGround()) _currentState = CharacterState.WALK; | |
} | |
void UpdateDeadState() | |
{ | |
} | |
bool IsOnGround() | |
{ | |
// Use raycast, line cast ... to know | |
// if character are on Platforms or not | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment