Example #1
0
 // Aiming perform
 private void HandleAiming()
 {
     // Aim input
     if (InputHandler.AimKeyHeld() && CanAim())
     {
         if (aimingProgress < weaponData.maxAimingProgress)
         {
             aimingProgress += CommonUtil.GetStepUpdate();
         }
     }
     else
     {
         if (aimingProgress > 0)
         {
             aimingProgress -= CommonUtil.GetStepUpdate();
         }
     }
     aimingProgress = Mathf.Clamp(aimingProgress, 0, weaponData.maxAimingProgress);
     // Aim cam zoom and weapon move
     AnimateAiming();
 }
Example #2
0
    void Update()
    {
        // Add some velocity
        veloctiy -= gravity * Time.deltaTime;

        // Ground check
        if (Physics.CheckSphere(groundCheckTransform.position, groundCheckDistance, groundMask))
        {
            isGrounded = true;
            veloctiy   = 0;
        }

        // Camera Input
        float mouseX = InputHandler.GetViewHorizontalAxis();
        float mouseY = -InputHandler.GetViewVerticalAxis();

        // Aiming
        if (InputHandler.AimKeyHeld())
        {
            aimValue = Mathf.Clamp(aimValue + CommonUtil.GetStepUpdate(), 0, maxAimValue);
        }
        else
        {
            aimValue = Mathf.Clamp(aimValue - CommonUtil.GetStepUpdate(), 0, maxAimValue);
        }
        mainCamera.transform.position = transform.position + mainCamera.transform.rotation * cameraRelative * (1 - maxAimMultiplier * aimValue / maxAimValue);

        // Shoot
        if (InputHandler.FireKeyHeld())
        {
            weaponData.TryShoot(mainCamera.transform);
        }

        // Rotation
        transform.RotateAround(this.transform.position, Vector3.up, mouseX);
        mainCamera.transform.RotateAround(this.transform.position, mainCamera.transform.right, mouseY);

        // Player Input
        float horizontal = InputHandler.GetHorizontalAxis();
        float vertical   = InputHandler.GetVerticalAxis();

        // Player rotate with camera
        transform.Rotate(new Vector3(0, mouseX, 0), Space.Self);

        // Flat movement speed calculation
        float spdLimiter = walkSpeed;

        if (InputHandler.SprintKeyHeld())
        {
            spdLimiter = runningSpeed;
        }
        else if (horizontal == 0 && vertical == 0)
        {
            spdLimiter = 0;
        }
        else if (vertical < 0)
        {
            spdLimiter = -walkSpeed;
        }
        currentSpeed = TweenLerpUtil.SpeedLerp(currentSpeed, spdLimiter, speedAcceleration);

        // === Player Jump ===
        if (InputHandler.JumpKeyDown() && isGrounded)
        {
            isGrounded = false;
            veloctiy  += initialJumpVelocity;
        }
        characterController.Move(veloctiy * Time.deltaTime * this.transform.up);

        // Apply motion
        Vector3 motion = transform.forward * vertical + transform.right * horizontal;

        motion.Normalize();
        motion = motion * Mathf.Abs(currentSpeed) * Time.deltaTime;

        // Apply movement
        characterController.Move(motion);

        // Apply animation
        animator.SetFloat("motionSpeed", currentSpeed);
        animator.SetFloat("jumpSpeed", veloctiy);
        animator.SetBool("isGrounded", isGrounded);
    }