Beispiel #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        Rigidbody rigidbody = GetComponent <Rigidbody>();

        //Get Correct Camera Transform
        Vector3    cameraFoward = Vector3.Scale(playerCamera.transform.forward, new Vector3(1.0f, 0.0f, 1.0f)).normalized;
        Quaternion cameraQuat   = Quaternion.LookRotation(cameraFoward, Vector3.up);

        if (InputManager.controllers.Count > 0)
        {
            InputDevice inputDevice = InputManager.controllers[0];

            //Get Inputs
            float hori = inputDevice.GetInput("MovementHorizontal");
            float verti = inputDevice.GetInput("MovementVertical");
            bool  jump = false;
            bool  sneakInput = false, walkInput = false;

            if (!lockMovements)
            {
                jump       = inputDevice.GetButtonWithLock("Jump");
                sneakInput = inputDevice.GetButtonWithLock("Sneak");
                walkInput  = inputDevice.GetButtonWithLock("Walk");

                if (sneakInput)
                {
                    sneakMode = !sneakMode;
                    walkMode  = false;
                }

                if (walkInput)
                {
                    walkMode  = !walkMode;
                    sneakMode = false;
                }
            }

            //Move the character in the direction of the camera
            float maxInput = Mathf.Max(Mathf.Abs(hori), Mathf.Abs(verti));

            Vector3 input = new Vector3(hori, 0f, -verti).normalized;

            Vector2 inputAmount = new Vector2(hori, verti);
            float   moveAmount  = Mathf.Clamp(inputAmount.magnitude, 0f, 1f);

            if (moveAmount == 0 || lockMovements)
            {
                float cacceleration = -expectedSpeed / brakeTime;
                expectedSpeed += (cacceleration * Time.fixedDeltaTime);

                if (Mathf.Abs(expectedSpeed) <= 0.1f)
                {
                    expectedSpeed = 0;
                }

                input          = lastDirection;
                animationSpeed = 0;
            }
            else
            {
                //Get Max Movement Speed
                float maxSpeed = moveSpeed;
                animationSpeed = 3;

                if (inWater)
                {
                    maxSpeed       = swimSpeed;
                    animationSpeed = 1;
                }
                else if (walkMode || maxInput < walkPercent)
                {
                    maxSpeed      *= walkSpeedPercent;
                    animationSpeed = 2;
                }
                else if (sneakMode)
                {
                    maxSpeed      *= tiptoeSpeedPercent;
                    animationSpeed = 1;
                }

                expectedSpeed += (moveAmount * acceleration) * Time.fixedDeltaTime;
                expectedSpeed  = Mathf.Clamp(expectedSpeed, 0f, maxSpeed);

                if (maxInput > 0.01f)
                {
                    lastDirection  = input;
                    lastCameraQuat = cameraQuat;
                }
            }

            Vector3 finalInput = cameraQuat * input * expectedSpeed;

            pointDirection = (cameraQuat * input).normalized;

            Vector3 accelerationVec = (Vector3.Scale(finalInput, new Vector3(1f, 0f, 1f)) - Vector3.Scale(rigidbody.velocity, new Vector3(1f, 0f, 1f))) / Time.fixedDeltaTime;
            rigidbody.AddForce(accelerationVec, ForceMode.Acceleration);

            //Do Jump
            if (jump && (!isFalling || inWater) && !jumpLock)
            {
                if (inWater)
                {
                    animator.SetTrigger("DoDolphinDive");
                    doneSplash = false;
                    DoSplash();
                }

                StartCoroutine(JumpOffset());
            }
        }

        RaycastHit hit;

        isFalling = !Physics.Raycast(transform.position, Vector3.down, out hit, isFallCheck, ~(LayerMask.GetMask("Water") | LayerMask.GetMask("Ignore Raycast")), QueryTriggerInteraction.UseGlobal);
        bool canLand = !Physics.Raycast(transform.position, Vector3.down, out hit, isLandedCheck, ~(LayerMask.GetMask("Water") | LayerMask.GetMask("Ignore Raycast")), QueryTriggerInteraction.UseGlobal);

        if (rigidbody.velocity.y <= 0f && jumpLock)
        {
            jumpLock  = false;
            isJumping = false;
            GetComponent <FootSteps>().DoJump();
        }

        RaycastHit waterHit;

        Physics.Raycast(transform.position + (Vector3.up * 5f), Vector3.down, out waterHit, Mathf.Infinity, LayerMask.GetMask("Water"), QueryTriggerInteraction.Collide);
        RaycastHit groundHit = new RaycastHit();

        Physics.Raycast(transform.position + (Vector3.up * 5f), Vector3.down, out groundHit, Mathf.Infinity, ~(LayerMask.GetMask("Water") | LayerMask.GetMask("Ignore Raycast")), QueryTriggerInteraction.Collide);

        overWater = (waterHit.point.y - groundHit.point.y > 0.6f);

        //Turn to Face Look Direction
        if (lastDirection != Vector3.zero)
        {
            //Do a bunch of angle checks, if we're moving alot presume we're spinning
            Quaternion finalRot = Quaternion.LookRotation(lastCameraQuat * lastDirection, Vector3.up);
            float      angle = Quaternion.Angle(transform.rotation, finalRot), averageAngle = 0f;
            float      currentTurnSpeed = turnSpeed;

            if (lastRotAngles.Count >= 40f)
            {
                lastRotAngles.Dequeue();
            }
            lastRotAngles.Enqueue(angle);

            foreach (float value in lastRotAngles)
            {
                averageAngle += value;
            }
            averageAngle /= lastRotAngles.Count;

            if (averageAngle > 50f)
            {
                currentTurnSpeed *= 3f;
            }

            transform.rotation = Quaternion.Lerp(transform.rotation, finalRot, Time.fixedDeltaTime * currentTurnSpeed);
        }

        // -- Do Animation --
        animator.SetInteger("Speed", animationSpeed);
        animator.SetBool("IsJumping", isJumping);
        animator.SetBool("HasJumped", jumpLock);
        animator.SetBool("IsRising", rigidbody.velocity.y > 0.5f);
        animator.SetBool("IsFalling", rigidbody.velocity.y < -0.1f && canLand);
        animator.SetBool("IsSwimming", inWater);
        animator.SetBool("IsThinking", isThinking);
        animator.SetBool("IsOverWater", overWater);

        //Do Particles
        if (!inWater && inWater != waterCheck)
        {
            waterTime = 5f;
            waterMaker.Stop();
            GetComponent <AudioSource>().PlayOneShot(splashExit);
        }

        if (inWater)
        {
            if (!movingWave.isPlaying)
            {
                movingWave.Play();
            }

            if (animationSpeed == 0)
            {
                if (waterMaker.clip != treadingSoundEffect)
                {
                    waterMaker.clip = treadingSoundEffect;
                    waterMaker.Play();
                }

                var emission = movingWave.emission;
                emission.rateOverTime = 1f;
            }
            else
            {
                if (waterMaker.clip != swimmingSoundEffect)
                {
                    waterMaker.clip = swimmingSoundEffect;
                    waterMaker.Play();
                }

                var emission = movingWave.emission;
                emission.rateOverTime = 4f;
            }
        }
        else
        {
            movingWave.Stop(true, ParticleSystemStopBehavior.StopEmitting);
        }

        waterCheck = inWater;

        if (!inWater && doneSplash)
        {
            doneSplash = false;
        }

        if (inWater && waterTime > 0f)
        {
            waterTime = 0f;
        }

        if (waterTime > 0f)
        {
            if (!waterDrops.isPlaying)
            {
                waterDrops.Play();
            }

            waterTime -= Time.fixedDeltaTime;
        }
        else
        {
            waterDrops.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
        }
    }
Beispiel #2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        Rigidbody rigidbody = GetComponent <Rigidbody>();

        //Get Correct Camera Transform
        Vector3    cameraFoward = Vector3.Scale(playerMovement.playerCamera.transform.forward, new Vector3(1.0f, 0.0f, 1.0f)).normalized;
        Quaternion cameraQuat   = Quaternion.LookRotation(cameraFoward, Vector3.up);

        if (InputManager.controllers.Count > 0)
        {
            InputDevice inputDevice = InputManager.controllers[0];

            //Get Inputs
            float hori   = inputDevice.GetInput("MovementHorizontal");
            float verti  = inputDevice.GetInput("MovementVertical");
            float height = inputDevice.GetIntInputWithDelay("MovementHeight", 0.3f, Time.fixedDeltaTime);

            Vector3 input = Vector3.zero;

            if (!isLocked)
            {
                input = new Vector3(hori, 0f, -verti).normalized;
            }

            Vector2 inputAmount = new Vector2(hori, verti);
            float   moveAmount  = Mathf.Clamp(inputAmount.magnitude, 0f, 1f);
            float   maxInput    = Mathf.Max(Mathf.Abs(hori), Mathf.Abs(verti));

            if (moveAmount == 0)
            {
                float cacceleration = -expectedSpeed / brakeTime;
                expectedSpeed += (cacceleration * Time.fixedDeltaTime);

                if (Mathf.Abs(expectedSpeed) <= 0.1f)
                {
                    expectedSpeed = 0;
                }

                input = lastDirection;
            }
            else
            {
                expectedSpeed += (moveAmount * acceleration) * Time.fixedDeltaTime;
                expectedSpeed  = Mathf.Clamp(expectedSpeed, 0f, moveAmount * moveSpeed);

                if (maxInput > 0.01f)
                {
                    lastDirection = input;
                }
            }

            Vector3 finalInput      = cameraQuat * input * expectedSpeed;
            Vector3 accelerationVec = (Vector3.Scale(finalInput, new Vector3(1f, 0f, 1f)) - Vector3.Scale(rigidbody.velocity, new Vector3(1f, 0f, 1f))) / Time.fixedDeltaTime;
            rigidbody.AddForce(accelerationVec, ForceMode.Acceleration);

            if (height != 0)
            {
                DoHeightInput(height);
            }

            //Do Height Sliding
            transform.position = Vector3.Lerp(transform.position, GetActualPos(), Time.deltaTime * lerpAmount);
        }
    }