Skip to content

Instantly share code, notes, and snippets.

@ThinhHB
Last active October 14, 2016 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThinhHB/9146abb460f729a02e07e513e96759ff to your computer and use it in GitHub Desktop.
Save ThinhHB/9146abb460f729a02e07e513e96759ff to your computer and use it in GitHub Desktop.
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