Files
uni/year3/semester1/CT3536: Games Programming/project/Assets/EngineSound.cs
2024-03-09 19:25:59 +00:00

34 lines
874 B
C#
Vendored

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// script to control the sound of the car's engine
public class EngineSound : MonoBehaviour
{
public AudioClip engineClip;
public float minPitch = 0.8f;
public float maxPitch = 1.2f;
public float minVolume = 0.5f;
public float maxVolume = 1.0f;
private AudioSource audioSource;
void Start()
{
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = engineClip;
audioSource.loop = true;
audioSource.Play();
}
void Update()
{
float speed = Mathf.Abs(Input.GetAxis("Vertical"));
float pitch = Mathf.Lerp(minPitch, maxPitch, speed);
float volume = Mathf.Lerp(minVolume, maxVolume, speed);
audioSource.pitch = pitch;
audioSource.volume = volume;
}
}