Skip to content

Commit 405c0ac

Browse files
committed
Added AnimationEvents (OnFootstep and OnFootLand plays audio)
1 parent 0fbf9cc commit 405c0ac

File tree

6 files changed

+82
-34
lines changed

6 files changed

+82
-34
lines changed

Assets/Samples/Character Controller/1.1.0-exp.10/Standard Characters/ThirdPerson/Scripts/ThirdPersonCharacterSystems.cs

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Unity.CharacterController;
77
using Unity.Mathematics;
88
using UnityEngine;
9+
using Random = UnityEngine.Random;
910

1011
[Serializable]
1112
public struct ThirdPersonCharacterData : IComponentData
@@ -70,12 +71,19 @@ struct KyleAnimationData : IComponentData
7071
// Read-Only Properties
7172
public float SpeedChangeRate;
7273
public float FallTimeout;
74+
public float FootstepAudioVolume;
7375

7476
// Read-Write Properties
7577
public float MotionBlend;
7678
public float FallTimeoutDelta;
7779
}
7880

81+
public class KyleAnimationManagedData : IComponentData
82+
{
83+
public AudioClip LandingAudioClip;
84+
public AudioClip[] FootstepAudioClips;
85+
}
86+
7987
[UpdateInGroup(typeof(SimulationSystemGroup))]
8088
[UpdateAfter(typeof(FixedStepSimulationSystemGroup))]
8189
[UpdateAfter(typeof(ThirdPersonPlayerVariableStepControlSystem))]
@@ -126,6 +134,7 @@ public void OnUpdate(ref SystemState state)
126134
ref var animationData = ref SystemAPI.GetComponentRW<KyleAnimationData>(characterData.AnimationEntity).ValueRW;
127135
var characterInput = characterAspect.CharacterInput.ValueRO;
128136
var characterBody = characterAspect.CharacterAspect.CharacterBody.ValueRO;
137+
var localTransform = characterAspect.CharacterAspect.LocalTransform.ValueRO;
129138
var targetSpeed = characterInput.SprintIsHeld ? characterData.SprintSpeed : characterData.WalkSpeed;
130139

131140
// Update Motion Blend
@@ -152,6 +161,23 @@ public void OnUpdate(ref SystemState state)
152161
else
153162
animator.SetBool(AnimIDFreeFall, true);
154163
}
164+
165+
// Handle Animation Events
166+
var animatorEvents = SystemAPI.ManagedAPI.GetComponent<KyleAnimatorEvents>(characterData.AnimationEntity);
167+
var managedData = SystemAPI.ManagedAPI.GetComponent<KyleAnimationManagedData>(characterData.AnimationEntity);
168+
while (animatorEvents.MoveNextFootstep())
169+
{
170+
if (managedData.FootstepAudioClips.Length > 0)
171+
{
172+
var index = Random.Range(0, managedData.FootstepAudioClips.Length);
173+
AudioSource.PlayClipAtPoint(managedData.FootstepAudioClips[index], localTransform.Position, animationData.FootstepAudioVolume);
174+
}
175+
}
176+
177+
while (animatorEvents.MoveNextLand())
178+
{
179+
AudioSource.PlayClipAtPoint(managedData.LandingAudioClip, localTransform.Position, animationData.FootstepAudioVolume);
180+
}
155181
}
156182
}
157183
}

Assets/Scenes/Playground/Overworld.unity

+15-2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@ MonoBehaviour:
188188
m_EditorClassIdentifier:
189189
SpeedChangeRate: 10
190190
FallTimeout: 0.15
191+
LandingAudioClip: {fileID: 8300000, guid: ff697d3070687ce4583faa0561a145a2, type: 3}
192+
FootstepAudioClips:
193+
- {fileID: 8300000, guid: 72f526a6a9890f643a88e85a61c86c8a, type: 3}
194+
- {fileID: 8300000, guid: 85016e0f2b01da248b9663dd49a161b0, type: 3}
195+
- {fileID: 8300000, guid: 186de84b3207156479abe98f4958fed0, type: 3}
196+
- {fileID: 8300000, guid: 1a91fcd19acf1e54bba0945d9f390849, type: 3}
197+
- {fileID: 8300000, guid: 14e8a8d2158bec840b56c54f5266e692, type: 3}
198+
- {fileID: 8300000, guid: 29841e7d5bbfb5b419c9ad16ca8bc4c1, type: 3}
199+
- {fileID: 8300000, guid: dd1af302b8902684d9381de1f2d3a5af, type: 3}
200+
- {fileID: 8300000, guid: 67c8b33e424ccdc4486edf538ab91c5a, type: 3}
201+
- {fileID: 8300000, guid: 274649b0e221539409070ebf6c18918b, type: 3}
202+
- {fileID: 8300000, guid: a3194b8bbc96ef84fab1f98f4b7dae3e, type: 3}
203+
FootstepAudioVolume: 0.5
191204
--- !u!1001 &1529753646
192205
PrefabInstance:
193206
m_ObjectHideFlags: 0
@@ -324,11 +337,11 @@ PrefabInstance:
324337
objectReference: {fileID: 0}
325338
- target: {fileID: 6689285571920954877, guid: 5fbcc801dea8fd640a004697d22ed448, type: 3}
326339
propertyPath: WalkSpeed
327-
value: 8
340+
value: 6
328341
objectReference: {fileID: 0}
329342
- target: {fileID: 6689285571920954877, guid: 5fbcc801dea8fd640a004697d22ed448, type: 3}
330343
propertyPath: SprintSpeed
331-
value: 15
344+
value: 11
332345
objectReference: {fileID: 0}
333346
- target: {fileID: 6689285571920954877, guid: 5fbcc801dea8fd640a004697d22ed448, type: 3}
334347
propertyPath: ControlledCamera

Assets/Scripts/Runtime/AnimatorSystem.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Unity.Transforms;
44
using UnityEngine;
55

6+
public interface IAddMonoBehaviourToEntityOnAnimatorInstantiation {}
7+
68
struct AnimatorInstantiationData : IComponentData
79
{
810
public UnityObjectRef<GameObject> AnimatorGameObject;
@@ -30,12 +32,19 @@ public void OnUpdate(ref SystemState state)
3032
.Build().ToEntityArray(state.WorldUpdateAllocator))
3133
{
3234
var data = SystemAPI.GetComponent<AnimatorInstantiationData>(entity);
33-
var spawnedAnimator = Object.Instantiate(data.AnimatorGameObject.Value).GetComponent<Animator>();
35+
var spawnedGameObject = Object.Instantiate(data.AnimatorGameObject.Value);
36+
var spawnedAnimator = spawnedGameObject.GetComponent<Animator>();
3437
state.EntityManager.AddComponentObject(entity, spawnedAnimator);
3538
state.EntityManager.AddComponentData(entity, new AnimatorCleanup
3639
{
3740
DestroyThisAnimator = spawnedAnimator
3841
});
42+
43+
foreach (var mb in spawnedGameObject.GetComponents<IAddMonoBehaviourToEntityOnAnimatorInstantiation>())
44+
{
45+
if (mb is MonoBehaviour monoBehaviour)
46+
state.EntityManager.AddComponentObject(entity, monoBehaviour);
47+
}
3948
}
4049

4150
// Sync the Animator's transform with the LocalToWorld

Assets/UnityTechnologies/StarterAssets/ThirdPersonController/Scripts/KyleAnimatorAuthor.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
[RequireComponent(typeof(AnimatorAuthor))]
55
public class KyleAnimatorAuthor : MonoBehaviour
66
{
7+
[Header("Kyle Animator Settings")]
78
public float SpeedChangeRate = 10.0f;
89
public float FallTimeout = 0.15f;
10+
11+
[Header("Kyle Animator Event Settings")]
12+
public AudioClip LandingAudioClip;
13+
public AudioClip[] FootstepAudioClips;
14+
[Range(0, 1)] public float FootstepAudioVolume = 0.5f;
15+
916
class KyleAnimatorAuthorBaker : Baker<KyleAnimatorAuthor>
1017
{
1118
public override void Bake(KyleAnimatorAuthor authoring)
@@ -14,7 +21,13 @@ public override void Bake(KyleAnimatorAuthor authoring)
1421
AddComponent(entity, new KyleAnimationData
1522
{
1623
SpeedChangeRate = authoring.SpeedChangeRate,
17-
FallTimeout = authoring.FallTimeout
24+
FallTimeout = authoring.FallTimeout,
25+
FootstepAudioVolume = authoring.FootstepAudioVolume
26+
});
27+
AddComponentObject(entity, new KyleAnimationManagedData
28+
{
29+
FootstepAudioClips = authoring.FootstepAudioClips,
30+
LandingAudioClip = authoring.LandingAudioClip
1831
});
1932
}
2033
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
using UnityEngine;
22

3-
public class KyleAnimatorEvents : MonoBehaviour
3+
public class KyleAnimatorEvents : MonoBehaviour, IAddMonoBehaviourToEntityOnAnimatorInstantiation
44
{
5-
void Start()
5+
// Count footsteps triggered
6+
int m_FootstepTriggerCount;
7+
public bool MoveNextFootstep()
68
{
7-
9+
if (m_FootstepTriggerCount <= 0)
10+
return false;
11+
m_FootstepTriggerCount--;
12+
return true;
813
}
14+
void OnFootstep(AnimationEvent animationEvent) => m_FootstepTriggerCount++;
915

10-
void OnFootstep(AnimationEvent animationEvent)
16+
// Count landings triggered
17+
int m_LandTriggerCount;
18+
public bool MoveNextLand()
1119
{
12-
13-
}
14-
15-
void OnLand(AnimationEvent animationEvent)
16-
{
17-
20+
if (m_LandTriggerCount <= 0)
21+
return false;
22+
m_LandTriggerCount--;
23+
return true;
1824
}
25+
void OnLand(AnimationEvent animationEvent) => m_LandTriggerCount++;
1926
}

Assets/UnityTechnologies/StarterAssets/ThirdPersonController/Scripts/ThirdPersonController.cs

-20
Original file line numberDiff line numberDiff line change
@@ -347,25 +347,5 @@ private void OnDrawGizmosSelected()
347347
new Vector3(transform.position.x, transform.position.y - GroundedOffset, transform.position.z),
348348
GroundedRadius);
349349
}
350-
351-
public void OnFootstep(float weight)
352-
{
353-
if (weight > 0.5f)
354-
{
355-
if (FootstepAudioClips.Length > 0)
356-
{
357-
var index = Random.Range(0, FootstepAudioClips.Length);
358-
AudioSource.PlayClipAtPoint(FootstepAudioClips[index], transform.TransformPoint(_controller.center), FootstepAudioVolume);
359-
}
360-
}
361-
}
362-
363-
public void OnLand(float weight)
364-
{
365-
if (weight > 0.5f)
366-
{
367-
AudioSource.PlayClipAtPoint(LandingAudioClip, transform.TransformPoint(_controller.center), FootstepAudioVolume);
368-
}
369-
}
370350
}
371351
}

0 commit comments

Comments
 (0)