Character walk with sound?

Asked by At

I'm a new programmer and am trying to put footsteps in my game. Every time the player isn't moving or is jumping, the sound should stop. The sound isn't playing at all right now, and the public bool isn't getting checkmarked. When I manually check the sound bool, the sound doesn't even play. :( I've looked everywhere and can't find anything! If anyone can help, I'd greatly appreciate it!

I've tried Play, Pause; but that just makes it play for a split second on awake.

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

public class sound : MonoBehaviour
{
    private CharacterController controller;
    public bool playSound;
    public AudioSource audioData;

    void Start()
    {
    audioData = GetComponent<AudioSource>();
    controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        SoundWalk();
        WalkSoundEnabler();
    }

    void WalkSoundEnabler()
    {
        if (controller.isGrounded && controller.velocity.magnitude > 1f)
        {
            playSound = true;
        }
        else
        {
            playSound = false;
        }
    }

    void SoundWalk()
    {
        if (! (playSound = true))
        {
            audioData.enabled = true;
            audioData.loop = true;
            Debug.Log("walking");
        }
        else
        {
            audioData.enabled = false;
            audioData.loop = false;
            Debug.Log("stopped");
        }
    }
}

I'd love a script that will play sound every time the player walks. If they're jumping or not moving, the sound shouldn't play.

0 Answers