Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }

        if (PauseController.Paused)
        {
            return; //the player can't do anything if the game is paused
        }

        //dashing code
        if (dashRetargetCooldown == 0 && !dashing) //code to find a new dash target
        {
            dashTarget = FindDashTarget();
            dashRetargetCooldown++;
        }
        else if (dashRetargetCooldown < 9) //only check for a new dash target every 10 frames to improve performance
        {
            dashRetargetCooldown++;
        }
        else
        {
            dashRetargetCooldown = 0;
        }

        if (Input.GetButtonDown("Dash") && dashTarget != null && !wallRunning && !controlsLocked)
        {
            animator.SetBool("Dashing", true);
            dashing = true;
            dashHitbox.SetActive(true);
            soundMan.PlaySwordSound(true);
            soundMan.PlayEffortSound(1);
            transform.rotation = Quaternion.LookRotation(dashTarget.transform.position - transform.position);
            transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
            if (healthMan != null)
            {
                healthMan.iFrames = Time.time + 99;
            }
        }
        if (dashing)
        {
            if (Vector3.Distance(transform.position, dashTarget.transform.position) <= dashTarget.GetComponent <DashTarget>().stopRadius || dashTarget == null)
            {
                animator.SetBool("Dashing", false);
                dashHitbox.SetActive(false);
                dashTarget.GetComponent <DashTarget>().cooldown = Time.time + 2.5f;
                dashTarget        = null;
                dashReticle.color = new Color(1, 1, 1, 0);
                dashing           = false;
                airTime           = Time.time + 0.25f + PlayerPrefs.GetInt("EasyDash", 0);
                if (healthMan != null)
                {
                    healthMan.iFrames = Time.time + 0.5f;
                }
            }
            if (dashing)
            {
                Vector3 dashDirection = dashTarget.transform.position - transform.position;
                controller.Move(Vector3.Normalize(dashDirection) * speed * 5 * Time.deltaTime);
            }
        }

        //========Moving code=============
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (dashing || controlsLocked) //if the player is dashing they cannot move until the dash is finished; if the controls are locked the player cannot move
        {
            x = 0;
            z = 0;
        }
        if (x != 0 || z != 0) // if the player moves they will stop floating
        {
            airTime = 0f;
        }

        if (wallRunning && wallRunDelay < Time.time) //if the player has not moved for more than half a second on a wall they will stop wallrunning
        {
            wallRunning = false;
            animator.SetBool("Wallrun", false);
            wallRunDelay = Time.time + 0.5f;
        }

        //rotate the player to the same direction as the camera when they move if they are not wallrunning or dashing
        if ((Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) && !wallRunning && !dashing && !controlsLocked)
        {
            transform.localRotation = Quaternion.Euler(0, mainCamera.transform.parent.localRotation.eulerAngles.y, 0);
        }
        //if the player is wallrunning they cannot move sideways or backwards
        if (wallRunning)
        {
            x = 0;
            if (z < 0)
            {
                z = 0;
            }
            else if (z > 0)          //if the player has moved while wallrunning reset the time they can stay on the wall
            {
                z           *= 1.5f; //increased speed while wallrunning
                wallRunDelay = Time.time + 0.5f;
            }
        }
        //attacking code
        if (comboTime > Time.time)
        {
            x /= 1 + (comboTime - Time.time) * 3;
            z /= 1 + (comboTime - Time.time) * 3;
        }
        if (comboTime < Time.time)
        {
            if (attacking)
            {
                attackCooldown = Time.time + 0.25f;
            }
            attacking  = false;
            comboState = 0;
            animator.SetInteger("Attacking", 0);
        }
        if (Input.GetButtonDown("Attack") && attackCooldown < Time.time && !dashing && !wallRunning && !controlsLocked)
        {
            attacking = true;
            animator.SetInteger("Attacking", comboState + 1);
            if (comboState == 0)
            {
                comboState++;
                comboTime = Time.time + 0.45f;
            }
            else if (comboState == 1)
            {
                comboTime += 0.5f;
                comboState++;
            }
            else if (comboState == 2)
            {
                comboTime     += 1f;
                attackCooldown = Time.time + 0.25f;
                comboState     = 0;
            }
        }

        Vector3 move = transform.right * x + transform.forward * z;

        if (Mathf.Abs(x) > 0.25f || Mathf.Abs(z) > 0.25f)
        {
            animator.SetBool("Moving", true);
        }
        else
        {
            animator.SetBool("Moving", false);
        }
        if (!wallRunning && !dashing && (Mathf.Abs(x) > 0.1f || Mathf.Abs(z) > 0.1f))
        {
            transform.GetChild(0).localRotation = Quaternion.Euler(0, Vector3.Angle(Vector3.forward, new Vector3(x, 0, z)) * Mathf.Sign(x), 0);
        }
        else if (wallRunning || dashing)
        {
            transform.GetChild(0).localRotation = Quaternion.Euler(0, 0, 0);
        }
        controller.Move(move * speed * Time.deltaTime);

        //========Jumping code============
        isGrounded = Physics.CheckBox(groundCheck.position, new Vector3(0.5f, 0.1f, 0.5f), Quaternion.Euler(0, 45, 0), groundMask);
        if (touchingTerrain || slideTime > Time.time && !wallRunning) //if the player has touched terrain this checks if they are on a steep slope and makes them slide down if they are, as well as disabling their jumps
        {
            float slope = terrain.terrainData.GetSteepness((transform.position.x - terrain.transform.position.x) / terrain.terrainData.size.x, (transform.position.z - terrain.transform.position.z) / terrain.terrainData.size.z);
            if (slope > 45)
            {
                isGrounded = false;
                doubleJump = false;
                Vector3 slideDirection = terrain.terrainData.GetInterpolatedNormal((transform.position.x - terrain.transform.position.x) / terrain.terrainData.size.x, (transform.position.z - terrain.transform.position.z) / terrain.terrainData.size.z);
                slideDirection.y = 0;
                controller.Move(slideDirection * Time.deltaTime);
                slideTime = Time.time + 0.05f;
            }
        }
        if (isGrounded)
        {
            fallAnimDelay = 0;
            animator.SetBool("Grounded", true);
            lastSafePosition = transform.position;
        }
        else
        {
            fallAnimDelay++;
            if (fallAnimDelay > 14)
            {
                animator.SetBool("Grounded", false);
            }
        }
        if ((isGrounded && velocity.y < 0) || wallRunning)
        {
            velocity.y = -2f;
            doubleJump = true;
        }
        if (airTime > Time.time || dashing)
        {
            velocity.y = -2f;
        }
        if (!wallRunning && !dashing)
        {
            if (Input.GetButtonDown("Jump") && (isGrounded || doubleJump) && !controlsLocked) //code for jump
            {
                animator.SetTrigger("Jump");
                soundMan.PlayEffortSound(0);
                if (isGrounded) //use the first jump
                {
                    isGrounded = false;
                }
                else //use the double jump
                {
                    doubleJump = false;
                }
                airTime    = 0f;
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
            }
            if (airTime < Time.time)
            {
                velocity.y += gravity * Time.deltaTime;
                if (velocity.y < gravity) //cap the falling speed
                {
                    velocity.y = gravity;
                }
                controller.Move(velocity * Time.deltaTime);
            }
        }
        else if (!dashing)
        {
            if (Input.GetButtonDown("Jump") && !controlsLocked) //wall jump
            {
                soundMan.PlayEffortSound(0);
                wallRunning  = false;
                wallRunDelay = Time.time + 0.25f;
                animator.SetBool("Wallrun", false);
                airTime    = 0f;
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
                velocity  += transform.right * (jumpHeight * -5 * Mathf.Sign(transform.InverseTransformPoint(lastWallrun.position).x));
            }
        }
        //reduce the horizontal force from wall jumping over time
        if (Mathf.Abs(velocity.x) > 0)
        {
            velocity.x -= (velocity.x + 2f) * Time.deltaTime * 2 + Mathf.Sign(velocity.x) * 0.1f;
            if (Mathf.Abs(velocity.x) < 1)
            {
                velocity.x = 0;
            }
        }
        if (Mathf.Abs(velocity.z) > 0)
        {
            velocity.z -= (velocity.z + 2f) * Time.deltaTime * 2 + Mathf.Sign(velocity.z) * 0.1f;
            if (Mathf.Abs(velocity.z) < 1)
            {
                velocity.z = 0;
            }
        }
    }