Exemplo n.º 1
0
    void Update()
    {
        Gamepad gamepad = Gamepad.current;

        if (gamepad == null || !allowInput)
        {
            return;
        }

        Vector2?stickInput = null;
        Vector2 leftStick  = gamepad.leftStick.ReadValue();

        if (leftStick.magnitude >= deadZone)
        {
            stickInput   = leftStick;
            arm.shoulder = leftShoulder;
        }
        else
        {
            Vector2 rightStick = gamepad.rightStick.ReadValue();
            if (rightStick.magnitude >= deadZone)
            {
                stickInput   = rightStick;
                arm.shoulder = rightShoulder;
            }
        }

        moveTarget         = stickInput != null ? stickInput.Value * moveAmount : Vector2.zero;
        transform.position = Vector2.SmoothDamp(transform.position, moveTarget, ref moveVelocity, moveDuration);

        if (!sword.IsSlashing())
        {
            if (stickInput != null)
            {
                SetAimAngle(stickInput.Value);
            }

            if ((arm.shoulder == leftShoulder && gamepad.leftShoulder.isPressed) ||
                (arm.shoulder == rightShoulder && gamepad.rightShoulder.isPressed))
            {
                float blockAngle = Util.Map(minAimAngle, maxAimAngle, minBlockAngle, maxBlockAngle, Mathf.Abs(aimAngle)) *
                                   (gamepad.leftShoulder.isPressed ? 1 : -1);
                sword.Block(blockAngle);
            }
            else if ((arm.shoulder == leftShoulder && gamepad.leftTrigger.wasPressedThisFrame) ||
                     (arm.shoulder == rightShoulder && gamepad.rightTrigger.wasPressedThisFrame))
            {
                sword.StartSlashing(aimAngle);
            }
            else
            {
                sword.PositionSword(0, aimAngle);
            }
        }
    }
Exemplo n.º 2
0
    void Update()
    {
        if (blockTimeRemaining > 0)
        {
            blockTimeRemaining -= Time.deltaTime;

            float blockAngle = Util.Map(minAimAngle, maxAimAngle, minBlockAngle, maxBlockAngle, aimAngle);
            sword.Block(blockAngle);
        }
        else if (!sword.IsSlashing())
        {
            nextTick -= Time.deltaTime;

            if (nextTick <= 0)
            {
                nextTick = tickLength;

                if (playerSword.IsSlashing())
                {
                    blockTimeRemaining = blockDuration;
                }
                else if (Random.Range(0f, 1f) < attackProbability)
                {
                    sword.StartSlashing(aimAngle);
                }
                else
                {
                    moveTarget = Quaternion.AngleAxis(Random.Range(-180f, 180f), Vector3.forward) * Vector2.up;

                    angleTarget = (Random.Range(0, angleRange) + (180 - angleRange) / 2) * Mathf.Sign(angleTarget);
                    if (Random.Range(0f, 1f) < switchProbability)
                    {
                        aimAngle    *= -1;
                        angleTarget *= -1;
                    }
                }
            }

            aimAngle = Mathf.SmoothDampAngle(aimAngle, angleTarget, ref angleVelocity, tickLength);
            aimIndicator.localPosition = Quaternion.AngleAxis(aimAngle, Vector3.forward) * Vector2.up * radius;

            sword.PositionSword(0, aimAngle);
        }

        transform.localPosition = Vector2.SmoothDamp(transform.localPosition, moveTarget, ref moveVelocity, moveDuration);
    }