예제 #1
0
    void Update()
    {
        // check for Y kill
        if (!isDead && transform.position.y < killHeight)
        {
            m_Health.Kill();
        }

        hasJumpedThisFrame = false;

        bool wasGrounded = isGrounded;

        GroundCheck();

        // landing
        if (isGrounded && !wasGrounded)
        {
            // Fall damage
            float fallSpeed      = -Mathf.Min(characterVelocity.y, m_LatestImpactSpeed.y);
            float fallSpeedRatio = (fallSpeed - minSpeedForFallDamage) / (maxSpeedForFallDamage - minSpeedForFallDamage);
            if (recievesFallDamage && fallSpeedRatio > 0f)
            {
                float dmgFromFall = Mathf.Lerp(fallDamageAtMinSpeed, fallDamageAtMaxSpeed, fallSpeedRatio);
                m_Health.TakeDamage(dmgFromFall, null);

                // fall damage SFX
                audioSource.PlayOneShot(fallDamageSFX);
            }
            else
            {
                // land SFX
                audioSource.PlayOneShot(landSFX);
            }
        }

        // crouching or sliding
        SetCrouchingState(m_InputHandler.GetCrouchInputHeld() && isGrounded && !isGrappling, isCrouching, false);
        //animator.SetBool("IsCrouching", isCrouching);

        //if crouching or sliding
        UpdateCharacterHeight(false);

        // handle cursor movement and wasd with chracter controller
        HandleCharacterMovement();

        if (!isGrappling)
        {
            HandleWallRun();
        }

        //update HUD stance
        UpdateStance();
    }
예제 #2
0
    public void Update()
    {
        // check for Y kill
        if (!isDead && transform.position.y < killHeight)
        {
            m_Health.Kill();
        }


        hasJumpedThisFrame = false;

        bool wasGrounded = isGrounded;

        GroundCheck();

        // landing
        if (isGrounded & !wasGrounded)
        {
            // Fall damage
            float fallSpeed      = -Mathf.Min(characterVelocity.y, m_LatestImpactSpeed.y);
            float fallSpeedRatio = (fallSpeed - minSpeedForFallDamage) / (maxSpeedForFallDamage - minSpeedForFallDamage);
            if (recievesFallDamage && fallSpeedRatio > 0f)
            {
                float dmgFromFall = Mathf.Lerp(fallDamageAtMinSpeed, fallDamageAtMaxSpeed, fallSpeedRatio);
                m_Health.TakeDamage(dmgFromFall, null);

                // fall damage SFX
                audioSource.PlayOneShot(fallDamageSFX);
            }
            else
            {
                // land SFX
                audioSource.PlayOneShot(landSFX, 0.3f);
            }
        }

        // crouching
        if (m_InputHandler.GetCrouchInputHeld())
        {
            SetCrouchingState(true, false);
        }
        else if (!m_InputHandler.GetCrouchInputHeld())
        {
            SetCrouchingState(false, false);
        }

        UpdateCharacterHeight(false);

        HandleCharacterMovement();

        if (Input.GetButton("Vertical"))
        {
            airAccel = 13.5f;
        }
        else
        {
            airAccel = 42f;
        }


        Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        // Get player velocity
        Vector3 playerVelocity = characterVelocity;

        // Slow down if on ground
        playerVelocity = CalculateFriction(playerVelocity);
        // Add player input
        playerVelocity += CalculateMovement(input, playerVelocity);
        // Assign new velocity to player object
        characterVelocity = playerVelocity;


        print(new Vector3(characterVelocity.x, 0f, characterVelocity.z).magnitude);
    }
예제 #3
0
    private void Update()
    {
        // shoot handling
        BasicWeapon activeWeapon = GetActiveWeapon();

        if (activeWeapon && m_WeaponSwitchState == WeaponSwitchState.Up)
        {
            // handle aiming down sights // if not grabbing
            isAiming = inter.canShoot && m_InputHandler.GetAimInputHeld();

            // handle shooting // if not grabbing
            if (inter.canShoot)
            {
                bool hasFired = activeWeapon.HandleShootInputs(
                    m_InputHandler.GetFireInputDown(),
                    m_InputHandler.GetFireInputHeld(),
                    m_InputHandler.GetFireInputReleased());

                // Handle accumulating recoil
                if (hasFired)
                {
                    m_AccumulatedRecoil += Vector3.back * activeWeapon.recoilForce;
                    m_AccumulatedRecoil  = Vector3.ClampMagnitude(m_AccumulatedRecoil, maxRecoilDistance);
                }
            }
            else
            {
                m_WeaponSwitchState = WeaponSwitchState.Down;
            }
        }

        if (m_InputHandler.GetCrouchInputHeld())
        {
            m_WeaponSwitchState = WeaponSwitchState.Down;
        }
        else if (m_WeaponSwitchState == WeaponSwitchState.Down)
        {
            m_WeaponSwitchState = WeaponSwitchState.Up;
        }

        // weapon switch handling
        if (!isAiming &&
            (activeWeapon == null || !activeWeapon.isCharging) &&
            (m_WeaponSwitchState == WeaponSwitchState.Up || m_WeaponSwitchState == WeaponSwitchState.Down))
        {
            int switchWeaponInput = m_InputHandler.GetSwitchWeaponInput();
            if (switchWeaponInput != 0)
            {
                bool switchUp = switchWeaponInput > 0;
                SwitchWeapon(switchUp);
            }
            else
            {
                switchWeaponInput = m_InputHandler.GetSelectWeaponInput();
                if (switchWeaponInput != 0)
                {
                    if (GetWeaponAtSlotIndex(switchWeaponInput - 1) != null)
                    {
                        SwitchToWeaponIndex(switchWeaponInput - 1);
                    }
                }
            }
        }

        // Pointing at enemy handling
        isPointingAtEnemy = false;
        if (activeWeapon)
        {
            if (Physics.Raycast(weaponCamera.transform.position, weaponCamera.transform.forward, out RaycastHit hit, 1000, -1, QueryTriggerInteraction.Ignore))
            {
                if (hit.collider.GetComponentInParent <EnemyController>())
                {
                    isPointingAtEnemy = true;
                }
            }
        }
    }