示例#1
0
    private void Update()
    {
        //stunning state
        stunCounter -= Time.deltaTime;
        stunned      = stunCounter > 0;

        stunDecal.SetActive(stunned);

        if (blocking)
        {
            return;
        }
        if (!stunned)
        {
            if (spi.controller.XDown)
            {
                Block();
                Invoke("UnBlock", blockingTime);
            }
        }

        //grounded state
        Collider[] colliders = Physics.OverlapSphere(jumpCheckPoint.position, 0.1f, floorMask);
        grounded = colliders.Length > 0;

        //jumping
        if (grounded && spi.controller.ADown)
        {
            rb.AddForce(Vector3.up * jumpingForce, ForceMode.Acceleration);
            pAnim.TriggerJumpAnimation();
            pAudioManager.PlayJumpSound();
        }

        //Dashing
        dashCooldownCounter -= Time.deltaTime;
        dashCounter         -= Time.deltaTime;

        dashing = dashCounter > 0;

        if ((spi.controller.L1Down || spi.controller.L2Down) && dashCooldownCounter < 0.0f)
        {
            dashCooldownCounter = dashCooldown;
            dashCounter         = dashDuration;
            pAudioManager.PlayDashSound();
        }

        //Rock pickup
        if (holding && rockBody != null)
        {
            //Move with the player
            rockBody.MovePosition(transform.position + transform.forward * holdingDistance + Vector3.up * holdingHeight);
            rockBody.rotation = transform.rotation;
            rockBody.GetComponent <Collider>().enabled = false;
            rockBody.useGravity = false;

            //Throw the rock
            if (spi.controller.YDown)
            {
                StartCoroutine(this.ThrowRock());
                pAnim.TriggerThrowAnimation();
            }
        }

        if (spi.controller.BDown) // Pick up / put down
        {
            bool prevHolding = holding;
            if (holding && rockBody != null)
            {
                //drop the rock
                rockBody.GetComponent <Collider>().enabled = true;
                rockBody.useGravity = true;
                holding             = false;
            }
            else
            {
                Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, pickupRange);

                for (int i = 0; i < nearbyObjects.Length; i++)
                {
                    if (nearbyObjects[i].tag == "Rock")
                    {
                        rockBody = nearbyObjects[i].GetComponent <Rigidbody>();
                        holding  = true;
                        break;
                    }
                    else if (nearbyObjects[i].tag == "Player" && nearbyObjects[i].GetComponent <PlayerController>().blocking&& nearbyObjects[i].gameObject != gameObject)
                    {
                        PlayerController otherPlayer = nearbyObjects[i].GetComponent <PlayerController>();

                        otherPlayer.rb.constraints = RigidbodyConstraints.None;
                        otherPlayer.beingHeldBy    = this;
                        otherPlayer.beingHeld      = true;

                        rockBody = nearbyObjects[i].GetComponent <Rigidbody>();
                        holding  = true;
                        break;
                    }
                }
            }

            if (prevHolding != holding)
            {
                pAnim.ToggleCarryAnimation();
            }
        }
    }