- Home /
Question by
bbeaver114 · Nov 20, 2021 at 09:19 PM ·
c#sfx
I need help with audio triggered by keypress
So I'm trying to make a first person game where the player can run and jump and stuff, when the player runs(presses shift) it would play an audiosource and keep looping that same sound. When the player jumps the sound would stop, and once the player lands and keeps running the audio will continue where it left off. I've looked all over the internet for things to make the script run how I want it but nothing has worked yet helpp this is basically what the sfx script looks like
{
AudioClip runclip;
AudioSource runsound;
float maxSpeed = 20;
bool jumping, sprinting, crouching, moving;
bool grounded;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
sprinting = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
sprinting = false;
}
if (sprinting)
{
maxSpeed = 40;
}
else
{
maxSpeed = 20;
}
if (crouching)
{
grounded = false;
}
if ((Input.GetButtonDown("Shift")) & moving & grounded)
{
runsound.PlayOneShot(runclip);
}
if ((Input.GetButtonUp("Shift")) || !moving || !grounded)
{
runsound.Stop();
}
}
}
Comment
Answer by unity_hosein · Nov 22, 2021 at 03:56 AM
void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
sprinting = true;
}
else
{
sprinting = false;
}
if (sprinting)
{
maxSpeed = 40;
}
else
{
maxSpeed = 20;
}
if (crouching)
{
grounded = false;
}
if ((Input.GetButton("Shift")) & moving & grounded)
{
runsound.PlayOneShot(runclip);
}
else if ((Input.GetButtonUp("Shift")) || !moving || !grounded)
{
runsound.Stop();
}
}
}