Unity 2D - How to change footstep sounds based on the type of surface.

Started by
6 comments, last by ferrous 7 years, 11 months ago

Hello everyone, I'm a newbie who want to get into game design. That said, I have next to no experience or knowledge about coding beside html and css.

Also, at the moment I'm learning how to use Unity. The goal at the moment is to make a simple prototype with a 2D pixel character moving around within a small map, doing some actions and interact with a couple of things. So far, I've managed to get the character up and moving around, done some collisions, together with some sound effects like footsteps, or playing music when she plays an instrument as long as I'm holding down the assigned key.

Now I want to take it a step further and change the sound of footsteps playing based on the type of ground she is walking on (footsteps on grass sound for grass surface, or footsteps on concrete for concrete surface, etc...). So far, all the tutorials that I've found were for 3D games, where they used different textures for each part of the surface, while in my prototype, the whole map is just a single pixel art spirte that I made in photoshop.

So, is there anyway to achieve this?

Advertisement
Well, I can think about 2 approaches at this moment:

1. Have an enum variable in your character and multiple triggers in the world. When the trigger throws, it sends the new enum value and the character updates its variable. Then, with a switch in the audio playing function, you choose what sound to play based on the enum. Another way of doing this is directly setting up the sound clip in the trigger, and setting player's current sound when the trigger enters.

2. Another approach would be to reproduce the sound outside of the player, in the ground or a collision box in the area you want that sound to reproduce. Then, while the player is inside, you reproduce the sound. This would be more problematic when stopping the sound because the player is not moving and in general a worse idea than the first one, but it's still another approach.

Good luck!

Thank you :D, I'm looking on how to make the first solution work. Unfortunately, Unity doesn't seem to allow more than one trigger - collider per gameObject. I think I can make it work by dividing the entire map into multiples gameObjects, one for each terrain and with its own trigger scripts. But it's a lot of work if I do it that way when it comes to mixed terrains, so I'm looking to see if there is any simpler way to do that.

P/S: Nailed it! Thank you jjimenezg93. Though if anyone have a better way to implement this, I'd be happy to listen.

Thank you :D, I'm looking on how to make the first solution work. Unfortunately, Unity doesn't seem to allow more than one trigger - collider per gameObject. I think I can make it work by dividing the entire map into multiples gameObjects, one for each terrain and with its own trigger scripts. But it's a lot of work if I do it that way when it comes to mixed terrains, so I'm looking to see if there is any simpler way to do that.

P/S: Nailed it! Thank you jjimenezg93. Though if anyone have a better way to implement this, I'd be happy to listen.


I'm glad it worked for you. Now you can create a prefab and you would just need to resize and select one audio clip for each surface.

Besides that, you can search bettet solutions now.

In many games the artists or designers who build the level create the data, usually as part of a navigation mesh.

In Unity for 3D worlds you can use "navigation areas" then read the current area type. If it matches a particular sound, use the sound with your motion code. You might have a shallow water motion sound, a hardwood floor motion sound, a rocky ground motion sound, etc.

Not sure of the best method for 2D, I've never done real work in that mode.

Based on what you said about your entire map being a single image, I assume you have a top-down view (Maybe you could post an image so we can see how it works, or at least describe it a little bit)?

Anyway, if I assume correctly, then you could create another image with the same size as your map, with the id of the surface type encoded into the color values. Then you could extract the pixel values from the image based on your character's position and get the surface type id from it. This way you could quickly paint the areas with the correct "color" in photoshop.

If my assumption was incorrect, then I will need some more information about how your map is represented.

Based on what you said about your entire map being a single image, I assume you have a top-down view (Maybe you could post an image so we can see how it works, or at least describe it a little bit)?

Anyway, if I assume correctly, then you could create another image with the same size as your map, with the id of the surface type encoded into the color values. Then you could extract the pixel values from the image based on your character's position and get the surface type id from it. This way you could quickly paint the areas with the correct "color" in photoshop.

If my assumption was incorrect, then I will need some more information about how your map is represented.

You're not wrong, I think. If I have to give an example, then I'll say that my prototype at the moment looks like Stardew Valley, if you're familiar with that game. Basically, I have a map made up from 32x32 tiles, put together in photoshop. The upcoming houses, trees, etc... will be put on top of that map with their own collider2D. .

@frob: Thank you for the solution :D. I'm looking at the documentation for "navigation areas" though I'm still new in both Unity and C# so how to actually use that feature is going to take some work.

Based on what you said about your entire map being a single image, I assume you have a top-down view (Maybe you could post an image so we can see how it works, or at least describe it a little bit)?

Anyway, if I assume correctly, then you could create another image with the same size as your map, with the id of the surface type encoded into the color values. Then you could extract the pixel values from the image based on your character's position and get the surface type id from it. This way you could quickly paint the areas with the correct "color" in photoshop.

If my assumption was incorrect, then I will need some more information about how your map is represented.

Or have a grid data structure, since they are using 32x32 tiles, that way they don't need an entire bitmap, and can calculate the tile they are under fairly easily. That data structure could also be used for other aspects of the game as well.

This topic is closed to new replies.

Advertisement