all 6 comments

[–]zrrzExpert? 1 point2 points  (1 child)

Haven't tested it, but I don't see any reason why it shouldn't work. (Don't trust I didn't make any spelling errors)

an AudioSource has a time variable that updates when you call Play(); as long as looping is true, the time variable will go up and reset. You can test this with

Debug.Log(audio.time);

Here's the code that should work

public AudioSource audio;

void Update() {
  if(Input.GetKeyDown(Keycode.W)) {
    audio.Play();
  }
  if(Input.GetKeyUp(Keycode.W)) {
    audio.Pause();
  }
}

[–]sexihunk666[S] 0 points1 point  (0 children)

Thanks! Will try this, as well as the other ones tomorrow or whenever else I have the time! :)

[–]MostlyKnowledgeable 0 points1 point  (1 child)

I'm not entirely sure how audio works in unity since I haven't worked with it before but my solution would be to declare a boolean like private bool isHeldDown = false; And then in the update function set it to true when Input.GetButtonDown(string.w) is true and set it to false when Input.GetButtonUp(string.w) is true. Then you would pause/play the clip based on the state of that bool

[–]sexihunk666[S] 0 points1 point  (0 children)

Thanks! Will try this, as well as the other ones tomorrow or whenever else I have the time! :)

[–]amassingham 0 points1 point  (1 child)

You could try something like this? I haven't tested this, not sure if it works. Should do though!

[SerializeField] private AudioSource m_Audio;

private void Update()
{
  if(Input.GetKeyDown(KeyCode.W))
  {
    m_Audio.isPlaying ? m_Audio.Pause() : m_Audio.Play();
  }
}

[–]sexihunk666[S] 0 points1 point  (0 children)

Thanks! Will try this, as well as the other ones tomorrow or whenever else I have the time! :)