UnityGameProjectsCode / NightwatchGame / PlayerController.cs
PlayerController.cs
Raw
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public KeyCode sprint, useFlashlight;

    public float walkSpeed, sprintSpeed, walkSoundDelay, sprintSoundDelay;
    private float movementSpeed, soundDelay, timeSinceStepSound = 0.0f;
    private int lastStepClip = -1;

    public bool lockCursor;
    private bool flashlightActive;
    private bool freezePlayer;
    private bool panLeft;

    public float lookSpeed;
    private float yaw;
    private float pitch;
    public float minYLook, maxYLook;

    private CharacterController cControl;
    private AudioSource stepSource;
    public AudioClip[] stepSounds = new AudioClip[7];
    private AudioSource flashLightAudio;
    public GameObject flashLight;
    public GameObject flashLightLight;
    public GameObject flashLightObject;
    private Animator animControl;

    //NEED TO FIX: controller moves faster if going diagonally.

    private void Awake()
    {
        pitch = transform.eulerAngles.x;
        yaw = transform.eulerAngles.y;
        cControl = GetComponent<CharacterController>();
        stepSource = GetComponent<AudioSource>();
        flashLightAudio = flashLight.GetComponent<AudioSource>();
        animControl = GetComponent<Animator>();
        animControl.enabled = false;

        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    private void Update()
    {
        if (!freezePlayer)
        {
            if (Input.GetKeyDown(useFlashlight) && flashlightActive)
            {
                flashLightLight.SetActive(!flashLightLight.activeSelf);
                flashLightObject.SetActive(!flashLightObject.activeSelf);

                if (flashLightObject.activeSelf)
                    flashLightAudio.pitch = 0.95f;
                else
                    flashLightAudio.pitch = 1.05f;

                flashLightAudio.Play();
            }

            //decide if sprinting or walking
            if (Input.GetKey(sprint))
            {
                movementSpeed = sprintSpeed;
                soundDelay = sprintSoundDelay;
            }
            else
            {
                movementSpeed = walkSpeed;
                soundDelay = walkSoundDelay;
            }

            if (cControl.velocity.magnitude > 0.0f)
            {
                timeSinceStepSound += Time.deltaTime;

                if (timeSinceStepSound >= soundDelay)
                {
                    int randomClip;

                    if (lastStepClip != -1)
                    {
                        do
                        {
                            randomClip = Random.Range(0, 7);
                        }
                        while (randomClip == lastStepClip);
                    }
                    else
                        randomClip = Random.Range(0, 7);

                    stepSource.clip = stepSounds[randomClip];
                    stepSource.pitch = Random.Range(0.90f, 1.10f);

                    if (!panLeft)
                    {
                        stepSource.panStereo = 0.5f;
                        panLeft = true;
                    }
                    else
                    {
                        stepSource.panStereo = -0.5f;
                        panLeft = false;
                    }

                    stepSource.Play();
                    timeSinceStepSound = 0.0f;
                    lastStepClip = randomClip;
                }
            }
            else
                timeSinceStepSound = 0.0f;

            //multiply the forward direction of the camera by the input from WASD
            Vector3 movement = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

            //call to the character controller to make the player object move. Use movementSpeed to determine how fast
            cControl.Move(movement * Time.deltaTime * movementSpeed);

            if (cControl.transform.position.y != 1.58f)
                cControl.transform.position = new Vector3(cControl.transform.position.x, 1.58f, cControl.transform.position.z);

            //get the input from the mouse to change the rotation of the camera
            yaw += lookSpeed * Input.GetAxis("Mouse X");
            //pitch -= lookSpeed * Input.GetAxis("Mouse Y");

            pitch = Mathf.Min(maxYLook, Mathf.Max(minYLook, pitch - (lookSpeed * Input.GetAxis("Mouse Y"))));
            Camera.main.transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
        }
    }

    public void TurnOnAnimator()
    {
        animControl.enabled = true;
    }

    public void DisableCursorLockForEnd()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }

    public void SetFlashlightActive(bool state)
    {
        flashlightActive = state;
        flashLight.SetActive(state);
    }

    public bool GetFlashlightState()
    {
        return flashLightLight.activeSelf;
    }

    public void SetFreezePlayer(bool state)
    {
        freezePlayer = state;
    }
}