//-------------------------------------------------------------------------------------- // IsGrounded: Check if the player is on the ground. // // Return: // bool: bool value for if the player is grounded or not. //-------------------------------------------------------------------------------------- bool IsGrounded() { // Cast a ray down from the player at the ground Debug.Log("IsGrounded"); Ray rRay = new Ray(transform.position - new Vector3(0, m_cPlayerCollider.height * 0.4f, 0), Vector3.down); RaycastHit rhHitInfo; // Set the layermask int nLayerMask = (LayerMask.GetMask("Ground")); // Is the ray colliding with the ground? if (Physics.Raycast(rRay, out rhHitInfo, 0.5f, nLayerMask)) { // reset jump state m_eJumpState = EJumpState.EJUMPSTATE_GROUNDED; // Return true and debug log the collider name Debug.Log(rhHitInfo.collider.name); return(true); } // Draw the ray cast and print ray information in the console Debug.DrawRay(rRay.origin, Vector3.down); Debug.Log(rRay.origin.ToString() + " " + rRay.direction.ToString()); // return false if not grounded return(false); }
//-------------------------------------------------------------------------------------- // Update: Function that calls each frame to update game objects. //-------------------------------------------------------------------------------------- void Update() { // if the game hasnt ended if (!ConveyorBelt.m_sbGameEnd) { // if space bar is pressed and the player is grounded if (Input.GetMouseButtonDown(0) && IsGrounded()) { // can jump bool is true m_bJump = true; m_bJumpAni = true; GetComponent <Animator>().SetBool("Jump", m_bJumpAni); } // if space bar is pressed and the player is grounded if (Input.GetMouseButtonDown(0) && m_eJumpState == EJumpState.EJUMPSTATE_SINGLE) { // can jump bool is true m_bJump = true; m_bJumpAni = true; // set jump state to double m_eJumpState = EJumpState.EJUMPSTATE_DOUBLE; } } // if jump animation is true if (m_bJumpAni) { // jump animation is false m_bJumpAni = false; // set animator bool GetComponent <Animator>().SetBool("Jump", m_bJumpAni); } }
//-------------------------------------------------------------------------------------- // FixedUpdate: This function is called every fixed framerate frame. //-------------------------------------------------------------------------------------- void FixedUpdate() { // Can the player jump? if (m_bJump) { // player cant jump m_bJump = false; // set jump state to single jump if grounded if (m_eJumpState == EJumpState.EJUMPSTATE_GROUNDED) { m_eJumpState = EJumpState.EJUMPSTATE_SINGLE; } // Add force to the player to jump m_rbRigidBody.AddForce(0, m_nForceConst, 0, ForceMode.Impulse); } }