-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathCollisionSound.ts
More file actions
62 lines (49 loc) · 1.77 KB
/
CollisionSound.ts
File metadata and controls
62 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Plays a sound when object collides with something.
* Add this to balls, rackets, or any object that should make sound on impact.
*/
@component
export class CollisionSound extends BaseScriptComponent {
@input
@hint("Sound to play on collision")
collisionSound: AudioComponent
@input
@hint("Minimum velocity magnitude to play sound (prevents tiny bumps from making noise)")
minVelocityThreshold: number = 5.0
@input
@hint("Volume multiplier based on impact speed (0-1)")
velocityToVolume: number = 0.05
private bodyComponent: BodyComponent | null = null
onAwake() {
// Get the body component
this.bodyComponent = this.getSceneObject().getComponent("Physics.BodyComponent")
if (!this.bodyComponent) {
print("CollisionSound: Physics.BodyComponent is required!")
return
}
// Set up audio for low latency
if (this.collisionSound) {
this.collisionSound.playbackMode = Audio.PlaybackMode.LowLatency
}
// Set up collision event
this.bodyComponent.onCollisionEnter.add(this.onCollisionEnter.bind(this))
}
/**
* Called when object collides with something
*/
private onCollisionEnter(e: CollisionEnterEventArgs) {
if (!this.collisionSound) return
// Get impact velocity
const velocity = this.bodyComponent ? this.bodyComponent.velocity : vec3.zero()
const speed = velocity.length
// Only play sound if impact is strong enough
if (speed < this.minVelocityThreshold) {
return
}
// Calculate volume based on impact speed (clamped to 0-1)
const volume = Math.min(1.0, speed * this.velocityToVolume)
print(`CollisionSound: Playing impact sound at volume ${volume.toFixed(2)} (speed: ${speed.toFixed(1)})`)
// Play the sound
this.collisionSound.play(volume)
}
}