/// <summary> /// Returns if an action was set or reset during the previous frame /// </summary> public bool GetIfActionHasChanged(eControllerActions action) { return((m_actionChanged & action) != 0); }
protected virtual void Update() { DoClimbing(); if (!m_isClimbing) { // Jumping Action if ( GetActionState(eControllerActions.Jump) && //GetIfActionHasChanged(eControllerActions.Jumping) && //NOTE: if not commented, player needs to release and press jump to jump m_jumpingTimer == -1 && //NOTE: if not commented, player jumps only once if holding jumping while in air IsGrounded) { m_jumpingTimer = JumpingAccTime; m_platformPhysics.VSpeed = JumpingSpeed; m_isGrounded = false; } else if (!GetActionState(eControllerActions.Jump)) { m_jumpingTimer = -1f; } // Moving Action if (GetActionState(eControllerActions.Right)) { m_platformPhysics.AddAcceleration(Vector2.right * HorizontalMovingAcc * m_horSpeedScale); } if (GetActionState(eControllerActions.Left)) { m_platformPhysics.AddAcceleration(-Vector2.right * HorizontalMovingAcc * m_horSpeedScale); } // Platform Drop Down Action if (m_isGrounded && m_platformDropTimer <= 0f && GetActionState(eControllerActions.PlatformDropDown)) { //NOTE: for this to work, OneWayCollisionDown should be removed from LayerCollisions m_smartCollider.LayerCollision = m_smartCollider.LayerCollision & ~m_smartCollider.OneWayCollisionDown; m_smartCollider.OneWayCollisionDown = 0; m_platformDropTimer = PlatformDropTime; } // Jumping if (m_jumpingTimer > 0f) { m_jumpingTimer -= Time.deltaTime; m_platformPhysics.Acceleration += transform.up * m_jumpingAcc; } // Platform Drop Down if (m_platformDropTimer > 0f) { m_platformDropTimer -= Time.deltaTime; if (m_platformDropTimer <= 0f) { // Restore the One Way Down collision again after the time is over m_smartCollider.OneWayCollisionDown = m_savedOneWayCollisionDown; } } m_instantVelocity = (transform.position - m_prevPos) / Time.deltaTime; m_prevPos = transform.position; m_platformPhysics.Drag = new Vector2(m_walkingDrag, 0f); m_platformPhysics.MaxHSpeed = MaxWalkingSpeed * m_horSpeedScale; m_platformPhysics.Position = transform.position; m_platformPhysics.UpdatePhysics(Time.deltaTime); m_groundDist = _CalculateGroundDist(); bool wasGrounded = m_isGrounded; m_isGrounded = m_smartCollider.enabled && m_smartCollider.IsGrounded() && m_platformPhysics.DeltaDisp.y <= 0f; // NOTE: Unity 5.4 has a bug in TransformDirection being affected by scale (x is negated to flip the sprite). // So instead of calling TransformDirection I will rotate it directly. transform.position = transform.position + transform.rotation * (m_platformPhysics.Position - transform.position); //+++ Do slopes // If over a slope, move the character in the slope direction if (m_slopeAngle != 0f) { if (Mathf.Abs(m_slopeAngle) <= Mathf.Round(m_maxSlope)) { transform.position += transform.up * m_platformPhysics.DeltaDisp.x * Mathf.Tan(m_slopeAngle * Mathf.Deg2Rad); m_isGrounded = true; } else if (Mathf.Sign(m_slopeAngle) == Mathf.Sign(m_platformPhysics.DeltaDisp.x)) { // Avoid climbing up the slope transform.position = m_platformPhysics.Position = new Vector3(m_prevPos.x, transform.position.y, transform.position.z); m_platformPhysics.HSpeed = 0f; m_isGrounded = true; } } // if not over a slope, check if there is a slope under the character to place it to the ground else if (!m_isGrounded && wasGrounded && m_platformPhysics.DeltaDisp.y <= 0f) { m_groundDist = _CalculateGroundDist(); float magneticDist = Mathf.Abs(m_platformPhysics.DeltaDisp.x * Mathf.Tan((m_maxSlope) * Mathf.Deg2Rad)); if (m_groundDist <= magneticDist) { m_isGrounded = true; m_groundDist += SmartRectCollider2D.k_SkinMinWidth; transform.position += -transform.up * m_groundDist; } } m_slopeAngle = 0f; //--- } //Update Actions m_actionChanged = m_prevActionFlags ^ m_actionFlags; m_prevActionFlags = m_actionFlags; }
/// <summary> /// Returns if an action is set /// </summary> public bool GetActionState(eControllerActions action) { return((m_prevActionFlags & action) != 0); }
/// <summary> /// Sets an action /// </summary> public void SetActionState(eControllerActions action, bool value) { m_actionFlags = (value ? (m_actionFlags | action) : (m_actionFlags & ~action)); }