Exemple #1
0
    // JumpingState = Animator.StringToHash("Jumping.Jumping");
    // FrontflipState = Animator.StringToHash("Jumping.Frontflip");
    // FrontflipLoopState = Animator.StringToHash("Jumping.FrontflipLoop");
    protected void Jumping(float elapsedTime)
    {
        // We only determine vertical speed (Horizontal Speed is maintained throughout the jump)
        if (MecanimAnimator.GetBool(MecanimHashes.Jump))
        {
            float jumpHeight = Settings.JumpHeight;
            if (CurrentState.fullPathHash == FrontflipState)
            {
                jumpHeight *= 1.5f;
            }
            VerticalSpeed = Mathf.Sqrt(2 * jumpHeight * Settings.Gravity);
            MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed);
            MecanimAnimator.SetBool(MecanimHashes.Jump, false);
        }
        else
        {
            ApplyGravity(elapsedTime);
        }

        // You can increase horizontal speed midair ONLY if doing a sneak jump and you weren't already at max speed
        if (CurrentState.fullPathHash == JumpingState && (CharInput.Left || CharInput.Right))
        {
            float prevSpeed = HorizontalSpeed;
            ApplyMovingHorizontal(elapsedTime * 0.5f);
            if (Mathf.Abs(HorizontalSpeed) < Mathf.Abs(prevSpeed))
            {
                HorizontalSpeed = prevSpeed;
            }
            else if (Direction.x > 0 && HorizontalSpeed > 0.79f * Settings.MaxHorizontalSpeed)
            {
                HorizontalSpeed = 0.79f * Settings.MaxHorizontalSpeed;
            }
            else if (Direction.x < 0 && HorizontalSpeed < -0.79f * Settings.MaxHorizontalSpeed)
            {
                HorizontalSpeed = -0.79f * Settings.MaxHorizontalSpeed;
            }
            SetMecanimAnimatorHorizontalSpeedFloat();
        }

        // You can grab onto walls while jumping
        MecanimAnimator.SetBool(MecanimHashes.GrabWall, CanGrabWall);

        // You can hang off most objects while jumping
        bool shouldHang = (CanHangOffObject && ActiveHangTarget.DoesFaceXAxis() && VerticalSpeed < 0) ||
                          (CanHangOffObject && ActiveHangTarget.DoesFaceZAxis() && CharInput.Up);

        MecanimAnimator.SetBool(MecanimHashes.Hang, shouldHang);

        // You can re-grab ropes you just let go of
        bool shouldRegrabRope = CharInput.Up && PreviousHangTarget != null && PreviousHangTarget.GetComponent <Collider>().bounds.Contains(transform.position);

        if (shouldRegrabRope)
        {
            AddHangTarget(PreviousHangTarget);
            MecanimAnimator.SetBool(MecanimHashes.ClimbRope, true);
        }
    }
Exemple #2
0
    // IdleState = Animator.StringToHash("Base Layer.Idle");
    protected virtual void Idle(float elapsedTime)
    {
        // Switch between crouching and standing idles based off enemies that could hear
        float idleNum    = MecanimAnimator.GetFloat(MecanimHashes.IdleNum);
        float targetIdle = GameManager.AI.EnemiesCouldHear > 0 ? 1 : 0;

        idleNum = Mathf.Lerp(idleNum, targetIdle, elapsedTime * IdleChangeSpeed);
        MecanimAnimator.SetFloat(MecanimHashes.IdleNum, idleNum);

        // You can start moving left or right at any point
        ApplyMovingHorizontal(elapsedTime);
        ApplyBiDirection();

        // You stick to the ground
        VerticalSpeed = GroundVerticalSpeed;
        MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed);

        // You could potentially fall down if the ground disappears beneath you
        MecanimAnimator.SetBool(MecanimHashes.Fall, !IsGrounded);

        // You can only pickup items while in idle
        if (CharInput.Pickup)
        {
            GameObject itemObj   = null;
            bool       canPickup = CanPickupItem(out itemObj);
            if (canPickup && itemObj != null)
            {
                _itemPickedup = itemObj.GetComponent <Item>();
            }
            MecanimAnimator.SetBool(MecanimHashes.Pickup, canPickup && _itemPickedup != null);
        }

        // You can re-grab ropes you just let go of
        bool shouldRegrabRope = CharInput.Up && PreviousHangTarget != null && PreviousHangTarget.GetComponent <Collider>().bounds.Contains(transform.position);

        if (shouldRegrabRope)
        {
            AddHangTarget(PreviousHangTarget);
            MecanimAnimator.SetBool(MecanimHashes.ClimbRope, true);
        }

        // You can jump
        AllowGroundJump();
    }
Exemple #3
0
    // RunningState = Animator.StringToHash("Ground.Running");
    protected void Locomotion(float elapsedTime)
    {
        // You run left or right
        ApplyMovingHorizontal(elapsedTime);
        ApplyBiDirection();

        // You stick to the ground
        VerticalSpeed = GroundVerticalSpeed;
        MecanimAnimator.SetFloat(MecanimHashes.VerticalSpeed, VerticalSpeed);

        // You could potentially fall down by running off a ledge
        MecanimAnimator.SetBool(MecanimHashes.Fall, !IsGrounded);

        // You could start rolling over small obstacles in your way
        MecanimAnimator.SetBool(MecanimHashes.ClimbLedge,
                                ActiveHangTarget != null && ActiveHangTarget is Ledge && ((Ledge)ActiveHangTarget).Obstacle &&
                                ((Direction.x > 0 && ((Ledge)ActiveHangTarget).Left) || (Direction.x < 0 && !((Ledge)ActiveHangTarget).Left)));

        // You can re-grab ropes you just let go of
        bool shouldRegrabRope = CharInput.Up && PreviousHangTarget != null && PreviousHangTarget.GetComponent <Collider>().bounds.Contains(transform.position);

        if (shouldRegrabRope)
        {
            AddHangTarget(PreviousHangTarget);
            MecanimAnimator.SetBool(MecanimHashes.ClimbRope, true);
        }

        // You can only do a backflip if you're running
        if (!IsSneaking && ((Direction.x > 0 && CharInput.JumpLeft) || (Direction.x < 0 && CharInput.JumpRight)))
        {
            MecanimAnimator.SetBool(MecanimHashes.Backflip, true);
            MecanimAnimator.SetBool(MecanimHashes.Jump, false);

            // You can do a jump
        }
        else
        {
            AllowGroundJump();
            MecanimAnimator.SetBool(MecanimHashes.Backflip, false);
        }
    }