Skip to content

Commit 9f61338

Browse files
committed
Add tutorial for footsteps playback on surface type
1 parent e951b68 commit 9f61338

File tree

9 files changed

+81
-1
lines changed

9 files changed

+81
-1
lines changed

manual/physics/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ Using built-in physics engine helps with creating realistic behaviour for game o
3737

3838
* [How to create a bouncing ball](tutorials/bouncing-ball.md)
3939
* [How to use a trigger](tutorials/use-trigger.md)
40-
40+
* [How to play footsteps on different surfaces](tutorials/footsteps.md)

manual/physics/tutorials/footsteps.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# HOWTO: Play footsteps on different surfaces
2+
3+
In this tutorial, you will learn how to create a script that will play different footstep sounds for the player depending on the surface type underneath. It uses raycasting down the player's feet to detect the physical material under the player and play matching sound effect. Script utilities `Tag` which improves readability of gameplay components (relation between physical material type and sound effect).
4+
5+
## 1. Create a script
6+
7+
Firstly we need to write a script (named for example `Footsteps`). It will handle the logic that detects the surface type and play the proper sound. To learn more about creating scripts see [this page](../../scripting/new-script.md).
8+
9+
```cs
10+
using System.Collections.Generic;
11+
using FlaxEngine;
12+
13+
public class Footsteps : Script
14+
{
15+
/// <summary>
16+
/// Maps surface type (Tag rom Physical Material) to the audio clip for footstep playback.
17+
/// </summary>
18+
public Dictionary<Tag, AudioClip> SoundPerSurfaceType = new();
19+
20+
/// <summary>
21+
/// Mask with all layers of the world object - except player (to prevent raycasting itself).
22+
/// </summary>
23+
public LayersMask WorldLayersMask = LayersMask.Default;
24+
25+
/// <summary>
26+
/// Sound volume.
27+
/// </summary>
28+
public float Volume = 1.0f;
29+
30+
public override void OnUpdate()
31+
{
32+
// This can exist in AnimEvent for animated character footsteps or in player movement script
33+
// For explanation purpose, run on Spacebar key
34+
if (!Input.GetKeyDown(KeyboardKeys.Spacebar))
35+
return;
36+
37+
// Raycast physical surface below the actor
38+
var feetLocation = Actor.Position;
39+
if (Physics.RayCast(feetLocation, Vector3.Down, out RayCastHit hit))
40+
{
41+
// Try get sound for a specific physical surface material (empty tag as a fallback)
42+
var tag = hit.Material ? hit.Material.Tag : new Tag();
43+
if (SoundPerSurfaceType.TryGetValue(tag, out AudioClip sound))
44+
{
45+
// Play sound at feet position (with auto-destroy)
46+
var source = new AudioSource
47+
{
48+
HideFlags = HideFlags.DontSave,
49+
Volume = Volume,
50+
Clip = sound,
51+
Position = feetLocation,
52+
};
53+
Level.SpawnActor(source);
54+
source.Play();
55+
Destroy(source, sound.Length);
56+
}
57+
}
58+
}
59+
}
60+
```
61+
62+
## 2. Setup a script
63+
64+
![Setup a Script](media/footsteps-properties.png)
65+
66+
Add a script to the created player or test actor, uncheck the layer with player actor from `WorldLayersMask`, and setup `SoundPerSurfaceType` to contain various surface types. That dictionary maps `Tag` into `AudioClip`. Tag has to match tag assigned in [Physical Material](../physical-material.md). It is a common practice to use namespaces for better organization of the surface types. For example, `Surface.Dirt`, `Surface.Metal` and so on.
67+
68+
![Physical Material Properties](media/footsteps-materials.png)
69+
70+
## 3. Test it out!
71+
72+
Hit **Play** button and test script by pressing `Spacebar` key to test sound when actor is above certain collider - it works with [Terrain](../../terrain/collision.md) layers too.

manual/physics/tutorials/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
* [How to create a bouncing ball](bouncing-ball.md)
44
* [How to use a trigger](use-trigger.md)
5+
* [How to play footsteps on different surfaces](footsteps.md)
Loading
Loading

manual/samples-tutorials/tutorials/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ You can use **Ctrl + F** to quickly find what you are looking for.
2323
* [How to spawn a prefab](../../get-started/prefabs/spawning-prefabs.md)
2424
* [How to create a bouncing ball](../../physics/tutorials/bouncing-ball.md)
2525
* [How to use a trigger](../../physics/tutorials/use-trigger.md)
26+
* [How to play footsteps on different surfaces](../../physics/tutorials/footsteps.md)
2627
* [How to create a decal](../../graphics/decals/create-decal.md)
2728
* [How to report a bug](../../contributing/report-a-bug.md)
2829
* [How to use raycasting](../../physics/raycasting.md)

manual/terrain/collision.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ Terrain uses heightfield collider per terain patch. You can specify the **Collis
66

77
If your game uses runtime-generated terrain then ensure to enable **Support Cooking At Runtime** option in Physics Settings to support generating heightfield data at runtime.
88

9+
## Physical Materials
10+
11+
![Terrain Physical Materials](media/terrain-physical-materials.png)
12+
13+
Terrain supports up to 8 different physical materials assigned for each Terrain Layer - can reflect material appearance.
Loading

manual/toc.md

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
## [Tutorials](physics/tutorials/index.md)
399399
### [How to create a bouncing ball](physics/tutorials/bouncing-ball.md)
400400
### [How to use a trigger](physics/tutorials/use-trigger.md)
401+
### [How to play footsteps on different surfaces](physics/tutorials/footsteps.md)
401402

402403
# [Particles](particles/index.md)
403404
## [Particle Emitter](particles/particle-emitter.md)

0 commit comments

Comments
 (0)