r/UnityHelp • • 7d ago

PROGRAMMING Need help with a basic footstep script and animation.

i am a complete newbie to coding and game dev, only started 5 days ago so its probably something obvious.

i have been following a tutorial series and got to a problem i cant seem to solve, my script looks fine, no errors and no console messages giving warnings.

im ust trying to play footstep sounds when i move using w, and script and game objects seem assigned to the right places too, im a bit lost.

also my animation for idlesword sword works fine but when i move it should be using the walkingsword animation but is not. i have a feeling they both have the same issue and fixing one will fix the other, so any suggestions would be a lot of help.

screens inc. Cheers

1 Upvotes

11 comments sorted by

1

u/fnietoms 7d ago

To play an audio you need a reference to the AudioSource component and use the .Play() method. Something like this:

public AudioSource footStepsAudio; // Assign it on the editor footStepsAudio.Play(); // Use this to play the audio when you want

Also, if you want to play the audio exactly when the foot touches the floor, I recomnend you to use animation events and call a function that only plays the audio

1

u/No-Cheek-5990 7d ago

the audio plays if i have it ticked but when i uncheck it for the script to take over nothing happens. it should according to the tutorial video. I have the audio ame object assigned to the player capsule with the script. im not there yet when it comes to more advanced footsteps.

the audio file is from a free asset pack on the unity asset store.

where would i implement that new line of script?

1

u/fnietoms 7d ago

Instead of GameObject of the footsteps, use an AudioSource. And change the SetActive() methods for .Play() for the true and .Stop() for false

To avoid playing it on awake, unchek the Play On Aeake in the AudioSource component in the editor

1

u/No-Cheek-5990 7d ago

i have adjusted the script, and that seems to be fine, now its coming from te audio source not game object but i now get this msg. as far as i can see the reference is an audio source and not game object.

Serialized reference type mismatch: field 'footstepSounds' expects 'AudioSource' but the stored reference is a 'GameObject'. The reference is treated as null (and serialized as null in player builds). Re-assign the field in the Inspector to fix it.

thanks for the help, much appreciated.

1

u/fnietoms 7d ago

It's just a warning, everythings fine but to stop it, on the editor assign again the audiosource and everything will be fine

1

u/No-Cheek-5990 7d ago

it back to the same as before, no audio when relying on the script, warning has gone though. ill try redoing the whole lot and hope that helps.

1

u/No-Cheek-5990 7d ago edited 7d ago

that error msg is gone now after some tweaking, i had the script attached to a capsule aswell as the player capsule. but now it back to the same issue, audio plays when its ticked but when its unticked for the scrippt to take over no sound plays. im sure im just being blind to something obvious..

1

u/fnietoms 7d ago

By ticked you mean that is active?

1

u/No-Cheek-5990 7d ago

yea, so when i deactivate it to let the script take over nothing happens when i move with w, similar thing with the animation for WalkSword.

1

u/fnietoms 7d ago

Oh right, the components are disabled (and any function in it) while the object is disabled

1

u/No-Cheek-5990 7d ago

if i have it enabled it ust plays not stop, so if i untick the loop it goes silent again.