Help i have been searching for hours for a simple way to detect if the player has stopped moving.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class soundManager : MonoBehaviour
{
    public AudioClip running;
    public AudioClip walking;
    private AudioSource Audio1;
    private AudioSource Audio2;

  
    private void Update()
    {
        Audio1 = GetComponent<AudioSource>();
        Audio1.clip = running;
        Audio1.loop = true;

        if (Input.GetKeyDown(KeyCode.W) && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift))
        {
            if (!Audio1.isPlaying)
            {
                Audio1.Play();
            }
        }
        if (Input.GetKeyDown(KeyCode.A) && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.A) && Input.GetKeyDown(KeyCode.LeftShift))
        {
            if (!Audio1.isPlaying)
            {
                Audio1.Play();
            }
        }

        if (Input.GetKeyDown(KeyCode.S) && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.S) && Input.GetKeyDown(KeyCode.LeftShift))
        {
            if (!Audio1.isPlaying)
            {
                Audio1.Play();
            }
        }

        if (Input.GetKeyDown(KeyCode.D) && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.D) && Input.GetKeyDown(KeyCode.LeftShift))
        {
            if (!Audio1.isPlaying)
            {
                Audio1.Play();
            }
        }

        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            Audio1.Stop();
        }

    }
}

currently this is what i have, so the running sound plays when either shift then WASD or WASD then shift is pressed. then once let go will stop. but i can run then take my hands of W A S or D while holding shift and it will still play the running sound
how should i fix this?

Definitely break ALL these things apart in to separate steps, one step at a time.

  • gather ALL of the input into temporary variables

  • print those temporary variables with Debug.Log() so you know they’re correct

  • act upon those variables to do things in your game, such as:

----> move the player

----> play sounds related to him moving (or stop those sounds)

In general, if you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

sorry but im really new to C#
and im not quite sure what you mean by that

also i just rly need to know how to stop the running sound when the character is stopped

I’m not really sure I can break it down any further in a text box on a forum.

You may wish to set this gently aside and go work on some basic Unity programming tutorials, such as these:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Here’s some great starter tutorials:

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Jason Weimann:

https://www.youtube.com/watch?v=OR0e-1UBEOU

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#:

https://www.youtube.com/watch?v=_cCGBMmMOFw

you could have a bool called is_moving then check the player velocity. when the player velocity is above a certain threshold… most likely 0, is_moving = true;
otherwise its false

This is kind if like asking how to play a guitar riff. To be able to do that you must first be able to fret chords, and to strum. If you don’t already know those things then the first step of the answer is to go and learn them.

Same deal here. If you’re not across how variables and statements work yet then the first step in turning off audio in certain circumstances is to learn those fundamentals. Even if we told you exactly what code to write and where (i.e. did it for you) this time, you’ll get blocked by those same things next time, and the time after, until you fill those gaps. There’s no time like the present. :slight_smile:

1 Like