Added collision sounds

This commit is contained in:
nothke 2024-08-20 01:12:54 +02:00
parent 5972019352
commit 5f5f43b3cf
10 changed files with 1616 additions and 2 deletions

View File

@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f998237f81278234b950080600e80b74, type: 3}
m_Name: hits_metallic
m_EditorClassIdentifier:
clips:
- {fileID: 8300000, guid: 8f2502b0ea356614faad568cc2ce8d28, type: 3}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1c204304ac4bfa44099474b89b8c2616
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -67,6 +67,7 @@ GameObject:
- component: {fileID: 5301951847822309914} - component: {fileID: 5301951847822309914}
- component: {fileID: 7933676651548381010} - component: {fileID: 7933676651548381010}
- component: {fileID: -6581473434499358903} - component: {fileID: -6581473434499358903}
- component: {fileID: 4959685768444209079}
m_Layer: 0 m_Layer: 0
m_Name: Basic Torus m_Name: Basic Torus
m_TagString: Untagged m_TagString: Untagged
@ -162,6 +163,24 @@ MonoBehaviour:
name: name:
rb: {fileID: 0} rb: {fileID: 0}
rotateAroundPoint: 0 rotateAroundPoint: 0
--- !u!114 &4959685768444209079
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7947812480369148219}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 075e36d3d3303934bac79dda1017c56c, type: 3}
m_Name:
m_EditorClassIdentifier:
profile: {fileID: 11400000, guid: 1c204304ac4bfa44099474b89b8c2616, type: 2}
volumeVelocityMult: 0.05
timeout: 0.05
pitchMult: 1
preventDoubleSound: 0
mixerGroup: {fileID: 0}
--- !u!1 &8478189424890482192 --- !u!1 &8478189424890482192
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -226,7 +226,7 @@ Transform:
m_GameObject: {fileID: 4060506572131258815} m_GameObject: {fileID: 4060506572131258815}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 20.26, y: 0, z: 28.17} m_LocalPosition: {x: 20.25, y: -0.07000017, z: 28.25}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
@ -1062,6 +1062,10 @@ PrefabInstance:
propertyPath: m_Name propertyPath: m_Name
value: crane_magent value: crane_magent
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 3485108705020879377, guid: 146ff579bcab1a343bdcbf24e8b5e462, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5436501012833944904, guid: 146ff579bcab1a343bdcbf24e8b5e462, type: 3} - target: {fileID: 5436501012833944904, guid: 146ff579bcab1a343bdcbf24e8b5e462, type: 3}
propertyPath: m_LocalPosition.x propertyPath: m_LocalPosition.x
value: 0 value: 0
@ -1150,7 +1154,7 @@ Rigidbody:
serializedVersion: 4 serializedVersion: 4
m_Mass: 50 m_Mass: 50
m_Drag: 1 m_Drag: 1
m_AngularDrag: 0.05 m_AngularDrag: 10
m_CenterOfMass: {x: 0, y: 0, z: 0} m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 85c191ec3cd67d6409ad30dde498cb96
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,84 @@
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
namespace Nothke.Audio
{
public class CollisionSounds : MonoBehaviour
{
const float RELATIVE_VELOCITY_TRESHOLD = 0.1f;
// impulse based, currently unused:
//const float IMPULSE_VOLUME_MULT = 0.00001f;
//const float IMPULSE_THRESHOLD = 1;
public CollisionSoundsProfile profile;
public float volumeVelocityMult = 0.05f;
public float timeout = 0.05f;
public float pitchMult = 1;
float lastTime;
public bool preventDoubleSound;
public AudioMixerGroup mixerGroup;
#if UNITY_EDITOR
private void Start()
{
if (!profile)
Debug.LogError("No collision sounds profile assigned", this);
}
#endif
public void ResetTimeout()
{
lastTime = Time.time;
}
void OnCollisionEnter(Collision collision)
{
if (!enabled)
return;
// Prevent sound in the first second
float time = Time.time;
if (time < 1)
return;
if (timeout > 0)
{
if (time - lastTime < timeout)
return;
lastTime = time;
}
if (preventDoubleSound)
{
var colSound = collision.collider.GetComponent<CollisionSounds>();
if (colSound)
colSound.ResetTimeout();
}
//float impulse = (collision.impulse / Time.fixedDeltaTime).magnitude;
//Debug.Log("Impulse: " + impulse);
//if (impulse < IMPULSE_THRESHOLD) return;
var relVel = collision.relativeVelocity.magnitude;
if (relVel < RELATIVE_VELOCITY_TRESHOLD)
return;
Debug.Assert(profile, "No collision sounds profile assigned", this);
// TODO: Move parameters to AudioManager:
profile.clips.Play(collision.GetContact(0).point,
volume: relVel * volumeVelocityMult,
pitch: Random.Range(0.95f, 1.05f) * pitchMult,
minDistance: 10, mixerGroup: mixerGroup);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 075e36d3d3303934bac79dda1017c56c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Nothke.Audio
{
[CreateAssetMenu(menuName = "CollisionSoundsProfile", fileName = "CollisionSoundsProfile")]
public class CollisionSoundsProfile : ScriptableObject
{
public AudioClip[] clips;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f998237f81278234b950080600e80b74
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: