using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Aim : MonoBehaviour
{
    public float rotationSpeed = 2.0f;
    public AudioSource audioSource;
    public AudioClip gearSound;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (gameObject.tag == "left")
        {
            if (Input.GetKey(KeyCode.E))
            {
                transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
                audioSource.clip = gearSound;
                if (!audioSource.isPlaying)
                {
                    audioSource.Play();
                }
            }

            if (Input.GetKeyUp(KeyCode.E))
            {
                audioSource.Stop();
            }
                
            if (Input.GetKey(KeyCode.D))
            {
                transform.Rotate(Vector3.forward * -rotationSpeed * Time.deltaTime);
                audioSource.clip = gearSound;
                if (!audioSource.isPlaying)
                {
                    audioSource.Play();
                }
            }

            if (Input.GetKeyUp(KeyCode.D))
            {
                audioSource.Stop();
            }
        }

        if (gameObject.tag == "right")
        {
            if (Input.GetKey(KeyCode.I))
            {
                transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
                audioSource.clip = gearSound;
                if (!audioSource.isPlaying)
                {
                    audioSource.Play();
                }
            }

            if (Input.GetKeyUp(KeyCode.I))
            {
                audioSource.Stop();
            }

            if (Input.GetKey(KeyCode.J))
            {
                transform.Rotate(Vector3.forward * -rotationSpeed * Time.deltaTime);
                audioSource.clip = gearSound;
                if (!audioSource.isPlaying)
                {
                    audioSource.Play();
                }
            }

            if (Input.GetKeyUp(KeyCode.J))
            {
                audioSource.Stop();
            }
        }
    }
}
