Help Creating Method for Run

Hello!

So to start, I’m new to all this and I cannot seem to figure this out haha. So I am currently trying to add in my run audio and play dust particles when I walk/run; however I am not sure how to do this as my run script is not in a method. Does Anyone know how I can put this into a method, please?

 void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }

        Flip();
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }

If it’s not in a method, can’t you just make a method?

Yea I realized that and I simply put it like this, but it still doesn’t play my audio or particles on run (it stays on):

 void Run()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
        CreateDust();
    }

Did you try looking up a tutorial? There’s plenty of videos on how to do this exact thing.

I ended up using this and it worked haha, ty though!

void Run()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
    }

If you talk about the music of the game, you can simply create an Audio Source component, and check “Play On Awake”.

If it’s a fast sound, for example my character is jumping…

You can use :

variable:

public AudioClip YourJumpSound;```

**In start:**
```audioSource = GetComponent<AudioSource>();```

**In update:**

```if Input...("Jump") && IsGrounded())
{
audioSource.PlayOneShot(YourJumpSound, 1);
}```

__For particles :__
**variables :**
```public ParticleSystem yourParticle;```

__in your code :__
__for play :__

```yourParticle.Play();```

__and__
__for stop :__

```yourParticle.Stop();```