So I had a little fun here and decided to make a bum instead of a rocket, propelled by its own farts. Not very mature I know but a lot of fun.
Being a little more familiar with Unity scripting already, I created an array of fart sounds to pick from. Here is my bum script in full (so far).
using UnityEngine;
public class Bum : MonoBehaviour
{
[SerializeField] float thrustStrength = 10f;
[SerializeField] AudioClip[] farts;
Rigidbody rb;
AudioSource audioSource;
AudioClip currentClip = null;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
ProcessInput();
}
private void ProcessInput()
{
if (Input.GetKey(KeyCode.Space))
{
rb.AddRelativeForce(Vector3.up * thrustStrength);
if (currentClip == null)
{
PlayNewClip();
}
else
{
if (!audioSource.isPlaying)
{
PlayNewClip();
}
}
}
else
{
StopCurrentClip();
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward);
}
}
void PlayNewClip()
{
currentClip = farts[Random.Range(0, farts.Length)];
if (currentClip != null && audioSource != null)
{
audioSource.clip = currentClip;
audioSource.Play();
}
}
void StopCurrentClip()
{
audioSource.Stop();
currentClip = null;
}
}
Might be useful for anyone else looking to pick a random audio clip from an array.