コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        // check for floors underneath, done via sphere collision check
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        // check death condition
        bool isDeaded = Physics.CheckSphere(groundCheck.position, groundDistance, deathMask);

        if (isDeaded)
        {
            handleDeath();
            return;
        }


        // check for collisions on left/right wallruns
        int WR_status = WR_script.checkWalls();

        // check for outof wallrunning
        if (WR_status == 0 && isWallRunning)
        {
            isWallRunning = false;
            GetComponent <WallRunning>().undoWallRun();
        }



        // USER INPUT SECTION

        float x             = Input.GetAxis("Horizontal");
        float y             = Input.GetAxis("Vertical");
        bool  hasMoveInput  = Mathf.Abs(x) > 0.1f || Mathf.Abs(y) > 0.1f;
        bool  SprintPressed = Input.GetKey(KeyCode.LeftShift);

        bool slideStatus = slide_script.checkSlide();

        //audio for sliding
        //if(hasMoveInput && isGrounded && slideStatus){
        //if(!slidingsound.isPlaying)
        //  {
        //    slidingsound.PlayOneShot(slidingsound.clip);
        //  }
        //  }

        //audio for walking
        if (hasMoveInput && isGrounded && !SprintPressed && !slideStatus)
        {
            if (!walkingsound.isPlaying)
            {
                walkingsound.PlayOneShot(walkingsound.clip);
            }
        }

        //audio for sprinting
        else if (hasMoveInput && isGrounded && SprintPressed && !slideStatus)
        {
            if (!sprintingsound.isPlaying)
            {
                sprintingsound.PlayOneShot(sprintingsound.clip);
            }
        }


        // ground movement
        if (isGrounded)
        {
            if (slideStatus)
            {
                slide_script.enterSlide();
                isSliding = true;
                //if(isliding){
                if (!slidingsound.isPlaying)
                {
                    slidingsound.PlayOneShot(slidingsound.clip);
                }
                //}
            }

            if (!isSliding)
            {
                if (hasMoveInput)
                {
                    // check for sprints
                    isSprinting = SprintPressed;
                    if (isSprinting)
                    {
                        moveSpeed = sprintSpeed;
                    }
                    else
                    {
                        moveSpeed = walkSpeed;
                    }

                    Vector3 move   = (transform.right * x + transform.forward * y) * acceleration;
                    float   premag = new Vector3(velocity.x, 0f, velocity.y).magnitude;
                    velocity.x += move.x;
                    velocity.z += move.z;

                    velocity = Vector3.ClampMagnitude(velocity, moveSpeed);
                }
                if (isWallRunning)
                {
                    // exit
                    isWallRunning = false;
                    WR_script.undoWallRun();
                }

                // reset Y velocity of we touch ground
                if (hasMoveInput && velocity.y < -0.5f)
                {
                    velocity.y = -0.5f; // make sure we 'stick' to ground
                }
                // apply ground friction
                if (!hasMoveInput)
                {
                    velocity.x /= groundFriction;
                    velocity.z /= groundFriction;
                }
            }
            else
            {
                slide_script.slideUpdate();
            }
        }
        else
        {
            // CHECK ENTER WALLRUN
            if (WR_status == 1 && !isWallRunning)
            {
                isWallRunning = true;
                WR_script.doWallRun(true);
            }
            if (WR_status == 2 && !isWallRunning)
            {
                isWallRunning = true;
                WR_script.doWallRun(false);
            }
            if (isWallRunning)
            {
                if (!walkingsound.isPlaying)
                {
                    walkingsound.PlayOneShot(walkingsound.clip);
                }
            }
            // Gravity
            if (!isWallRunning)
            {
                velocity.y += gravity * Time.deltaTime;  //  x = 1/2 a t^2
            }
            else
            {
                WR_script.inWallRunUpdate();
            }

            //// if sliding, stop and undo
            //if (isSliding)
            //{
            //    if(slide_script.exitSlide())
            //        isSliding = false;
            //}
        }
        // jumping
        if (Input.GetButtonDown("Jump"))
        {
            landed = true;
            if (isGrounded && !isSliding)
            {
                JumpFromGrounded(x, y);

                //audio for jumping
                if (!jumpingsound.isPlaying)
                {
                    jumpingsound.PlayOneShot(jumpingsound.clip);
                }

                isGrounded  = false;
                isSprinting = false;
            }
            else if (isGrounded && isSliding)
            {
                if (slide_script.jumpFromSlide())
                {
                    isSliding = false;
                }

                //audio for jumping
                if (!jumpingsound.isPlaying)
                {
                    jumpingsound.PlayOneShot(jumpingsound.clip);
                }
            }
            else if (isWallRunning)
            {
                if (WR_script.jumpExitWallRun())
                {
                    isWallRunning = false;
                }

                //audio for jumping
                if (!jumpingsound.isPlaying)
                {
                    jumpingsound.PlayOneShot(jumpingsound.clip);
                }
            }
            else
            {
                // walljump boost check
                WR_script.wallJumpBackBoost();

                //audio for jumping
                if (!jumpingsound.isPlaying)
                {
                    jumpingsound.PlayOneShot(jumpingsound.clip);
                }
            }
        }

        if (isGrounded && landed)
        {
            if (!landingsound.isPlaying)
            {
                landingsound.PlayOneShot(landingsound.clip);
            }

            landed = false;
        }



        // apply movement
        controller.Move(velocity * Time.deltaTime);
    }