Exemple #1
0
    private bool TryReachLedge(PlayerController player, float zVel, ref float yVel, bool allowClearingBoost = true)
    {
        // Doing standing jump, allow player to grab if close
        if (zVel < 0.5f)
        {
            zVel = 0.5f;
        }

        // Need to check where player will be relative to ledge
        float   timeAtPeak   = yVel / player.Gravity; // v = u + at
        Vector3 relative     = ledgeInfo.Point - player.transform.position;
        float   displacement = UMath.GetHorizontalMag(relative);
        // Maximum to account for when player hits wall but keeps moving up
        float timeAtLedge = Mathf.Max(UMath.TimeAtHorizontalPoint(zVel, displacement), timeAtPeak);
        float vertPos     = UMath.PredictDisplacement(yVel, timeAtLedge, -player.Gravity);

        if (vertPos >= relative.y - 2.6f && vertPos <= relative.y - 0.8f) // Distance is great, do auto-grab
        {
            player.StateMachine.GoToState <AutoGrabbing>(ledgeInfo);
            return(true);
        }
        else if (allowClearingBoost && vertPos < relative.y && vertPos > relative.y - 0.8f) // she hits it around the hip just adjust up velocity to clear it
        {
            float   time;
            Vector3 adjustedVel = UMath.VelocityToReachPoint(player.transform.position,
                                                             ledgeInfo.Point, zVel, player.Gravity, out time);
            yVel = adjustedVel.y;
        }

        return(false);
    }