Question by
taylormade2g · May 17, 2016 at 03:39 AM ·
unity 2djumpingplayer movementdouble jump
Hi everyone I'm trying to make my player double jump but I'm new to C#. Here what I got so far? Can anyone help?
using UnityEngine; using System.Collections;
using System; using System.Collections.Generic;
public class PlayerStateMachine : MonoBehaviour { private enum PlayerStates { IDLE, RUN, ENTER_JUMP, IN_AIR,
NUM_STATES
}
[SerializeField]
private float maxJumpBtnHoldTime = 1.0f;
[SerializeField]
private float timeHoldingInput = 0.0f;
[SerializeField]
private Transform groundCheck;
[SerializeField]
private LayerMask walkableLayer;
[SerializeField]
private float groundCheckRadius = 0.1f;
Dictionary<PlayerStates, Action> fsm = new Dictionary<PlayerStates, Action>();
Player thePlayer;
private PlayerStates curState;
private bool canJump = true;
private bool onGround = false;
// Use this for initialization
void Start ()
{
thePlayer = GetComponent<Player> ();
if (thePlayer == null)
{
Debug.Log ("PlayerStateMachine:Start() - We could retrieve the Player comoponent");
}
fsm.Add ( PlayerStates.IDLE, StateIdle);
fsm.Add (PlayerStates.RUN, StateRun);
fsm.Add ( PlayerStates.ENTER_JUMP, StateEnterJump);
fsm.Add (PlayerStates.IN_AIR, StateInAir);
SetState( PlayerStates.IDLE );
}
// Update is called once per frame
void Update ()
{
fsm [curState].Invoke ();
}
//-------------------------------------------------------------
// Helper Functions
void CheckForGround()
{
onGround = Physics2D.OverlapCircle( groundCheck.position, groundCheckRadius, walkableLayer);
}
void HandleLandOnGround ()
{
float direction = Input.GetAxis ("Horizontal");
canJump = true;
if (direction != 0f)
{
SetState (PlayerStates.RUN);
}
else
{
SetState (PlayerStates.IDLE);
}
}
void HandleHorizontalInput()
{
float direction = Input.GetAxis ("Horizontal");
Vector2 accel = new Vector2 (thePlayer.RunAccel() * direction, 0 );
if (direction != 0f)
{
GetComponent<Rigidbody2D>().velocity += accel;
}
float magnitude = Mathf.Abs( GetComponent<Rigidbody2D>().velocity.x );
if (magnitude <= 0.01f)
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (0, GetComponent<Rigidbody2D>().velocity.y);
}
else if ( magnitude >= thePlayer.MaxRunSpeed() )
{
GetComponent<Rigidbody2D>().velocity = new Vector2( thePlayer.MaxRunSpeed() * direction, GetComponent<Rigidbody2D>().velocity.y);
}
}
//---------------------------------------------------------------------------
//State Functions
void SetState( PlayerStates nextState )
{
if (nextState != curState)
{
Debug.Log (nextState);
timeHoldingInput = 0;
curState = nextState;
}
}
void StateIdle()
{
CheckForGround ();
if (onGround == false)
{
SetState (PlayerStates.IN_AIR);
}
else if (Input.GetAxis ("Horizontal") != 0f)
{
SetState (PlayerStates.RUN);
}
else if (Input.GetKeyDown (KeyCode.Space))
{
SetState (PlayerStates.ENTER_JUMP);
}
}
void StateRun()
{
float direction = Input.GetAxis ("Horizontal");
HandleHorizontalInput ();
CheckForGround ();
if (onGround == false)
{
SetState (PlayerStates.IN_AIR);
}
else if (Input.GetKeyDown (KeyCode.Space))
{
SetState (PlayerStates.ENTER_JUMP);
}
else if (direction == 0f)
{
SetState (PlayerStates.IDLE);
}
}
void StateEnterJump()
{
GetComponent<Rigidbody2D>().velocity = new Vector2( GetComponent<Rigidbody2D>().velocity.x, thePlayer.InitJumpSpeed () );
SetState( PlayerStates.IN_AIR );
}
void StateInAir()
{
HandleHorizontalInput ();
if( canJump )
{
if (canJump && timeHoldingInput < maxJumpBtnHoldTime && Input.GetKey (KeyCode.Space))
{
timeHoldingInput += Time.deltaTime;
GetComponent<Rigidbody2D> ().velocity += new Vector2 (0, thePlayer.JumpAccel ());
}
else
{
canJump = false;
}
}
else
{
CheckForGround();
if( onGround )
{
HandleLandOnGround ();
}
}
}
}
Comment