/// <summary> /// Called when the movement loses control. Override to do any reset type actions. /// </summary> override public void LosingControl() { pushDirection = 0; pushable = null; // Because we use another movement to do the movement we call LosingControl on it too to ensure any values are reset there too if (enabled) { character.DefaultGroundMovement.LosingControl(); } }
/// <summary> /// Gets a value indicating whether this movement wants to control the movement on the ground. /// This movement wants control if the side colliders are pushing against a box. /// </summary> /// <value><c>true</c> if this instance wants control; otherwise, <c>false</c>.</value> override public bool WantsGroundControl() { if (!enabled) { return(false); } if (character.DefaultGroundMovement == this) { Debug.LogError("The Push movement can't be the default ground movement. Disabling movement."); enabled = false; } // Early out, only push if grounded. if (!character.Grounded) { return(false); } // Early out, only push if pressing in a direction. if (character.Input.HorizontalAxisDigital == 0) { return(false); } // Early out, if last frame we were pusing one way, we want to lose control for at least one frame before pushing another way if (pushDirection == -character.Input.HorizontalAxisDigital) { return(false); } int hitCount = 0; Pushable matchingPushable = null; float pushableDistance = 1; float nonPushableDistance = 1; RaycastType typeToCheck = (character.Input.HorizontalAxisDigital == 1 ? RaycastType.SIDE_RIGHT : RaycastType.SIDE_LEFT); initialPushOffset = 0; for (int i = 0; i < character.Colliders.Length; i++) { if (character.Colliders[i].RaycastType == typeToCheck) { RaycastHit2D hit = character.GetClosestCollision(i); if (hit.collider != null) { Pushable currentPushable = hit.collider.GetComponent <Pushable>(); // Pushing against something that isn't pushable TODO add distance checks. if (currentPushable == null) { if (hit.fraction < nonPushableDistance) { nonPushableDistance = hit.fraction; } } else { if (currentPushable != matchingPushable) { // Found a closer pushable (or if matching pushable is null we just found a pushable) if (hit.fraction < pushableDistance) { pushableDistance = hit.fraction; matchingPushable = currentPushable; initialPushOffset = (hit.fraction * (character.Colliders[i].Length + character.Colliders[i].LookAhead)) - character.Colliders[i].Length - character.Colliders[i].LookAhead; hitCount = 1; } } else { hitCount++; } } } } } if (matchingPushable != null && pushableDistance < nonPushableDistance && initialPushOffset < 0) { pushable = matchingPushable; initialPushOffset *= -character.Input.HorizontalAxisDigital; pushDirection = character.Input.HorizontalAxisDigital; return(true); } return(false); }