Ejemplo n.º 1
0
    // Called every frame after Start().
    private void Update()
    {
        // Only allow the Player to rotate if the Escape Menu is not active.
        if (!localNetworkUser.showEscapeMenu)
        {
            rotationX += Input.GetAxis("Mouse X") * sensitivity;
            rotationX  = ClampAngle(rotationX, minimum, maximum);

            rotationY += Input.GetAxis("Mouse Y") * sensitivity;
            rotationY  = ClampAngle(rotationY, minimum, maximum);

            Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
            Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);

            switch (rotationAxis)
            {
            case RotationAxis.X:
                transform.localRotation = originalRotation * xQuaternion;
                break;

            case RotationAxis.Y:
                transform.localRotation = originalRotation * yQuaternion;
                break;

            default:
                Debug.LogWarning("No rotation axis selected.");
                break;
            }
        }

        if (lastRotation != transform.localRotation)
        {
            // Called if the Player has rotated to a new rotation.

            lastRotation = transform.localRotation;

            localNetworkUser.BreakIdle();
            idleTimer = 0f;
        }
        else
        {
            // Called if the Player has not changed his rotation (starts count down to idle).

            idleTimer += Time.deltaTime;

            if (idleTimer >= localNetworkUser.idleTime)
            {
                // Set the player as idle.
                animator.SetBool("Idle", true);
                idleTimer = 0f;
            }
        }
    }
Ejemplo n.º 2
0
    // Called every frame after Start().
    private void Update()
    {
        // Only the owner of this Player instance can control this component.
        if (!isLocalPlayer)
        {
            return;
        }

        // Updated the movement animation float, based on the movement velocity.
        animator.SetFloat("Movement Speed", movementDirection.magnitude);

        // Only allow the Player to move if the Escape Menu is not activate.
        if (!localNetworkUser.showEscapeMenu)
        {
            currentSpeed = Input.GetButton("Sprint") ? sprintingSpeed : movementSpeed;

            movementDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
            movementDirection = transform.TransformDirection(movementDirection);

            transform.position += movementDirection * currentSpeed * Time.deltaTime;

            if (movementDirection.magnitude > 0.5f)
            {
                if (stepTimer >= 1 / currentSpeed)
                {
                    // Add a variation to the step sound pitch.
                    float randomPitch = Random.Range(0.9f, 1.3f);

                    // Randomly select a step sound from <stepSounds>.
                    int stepSoundIndex = Random.Range(0, stepSounds.Length);

                    // Play the step sound over the Network.
                    CmdPlayStepSound(stepSoundIndex, randomPitch);
                    stepTimer = 0f;
                }
                else
                {
                    stepTimer += Time.deltaTime;
                }
            }
        }
        else
        {
            movementDirection = Vector3.zero;
        }

        // If the Player moves, break his idle.
        if (lastMovementPosition != transform.position)
        {
            lastMovementPosition = transform.position;

            localNetworkUser.BreakIdle();
            idleTimer = 0f;
        }
        else
        {
            idleTimer += Time.deltaTime;

            if (idleTimer >= localNetworkUser.idleTime)
            {
                animator.SetBool("Idle", true);
                idleTimer = 0f;
            }
        }
    }