I’m working on this project and I want to have the player slow down while the trigger to play the audio clip is going. I’m very new to scripting so I’m not sure what I’m doing wrong. Here is the script I have. Any help is appreciated!
using UnityEngine;
using System.Collections;
public class PlaySound : MonoBehaviour
{
public AudioClip SoundToPlay;
public float Volume;
AudioSource audio;
public bool alreadyPlayed = false;
public float movementSpeed = 10f;
void Start()
{
movementSpeed = 5f;
audio = GetComponent<AudioSource>();
movementSpeed = 10f;
}
void OnTriggerEnter()
{
if (!alreadyPlayed)
{
audio.PlayOneShot(SoundToPlay, Volume);
alreadyPlayed = true;
}
}
}
In the OnTriggerEnter, inside the !alreadyPlaying if block lower the movementspeed and in an OnTriggerExit put it back to the original value.
Also it is a good idea if you make the movementSpeed private, add a public normalMovementSpeed and slowMovementSpeed and in the Start function make the movementSpeed equal to the normalMovementSpeed’s value. Also when you make it slow and make it normal, use the slowMovementSpeed and normalMovementSpeed respectively. This way you can tweak these values from the inspector and you won’t mess it up with hardcoded values in your code.
ps: I think I go for a nap or something. Sorry, I completely misconstrued the entire situation here.
I’d switch line 20 so you use Play instead of PlayOneShot. Then you can just check isPlaying to determine whether you want the player to go fast or slow from Update. (I don’t believe isPlaying works with PlayOneShot)
I think the PlayOneShot is there to make sure the audio doesn’t play again after playing the first time. I’m incredibly new at this and the professor isn’t super helpful so I’m just winging it here lol