OnCollideSound

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(small update to make sure (potentially long acting) sound is attached to GO by default)
Line 13: Line 13:
 
<csharp>
 
<csharp>
 
using UnityEngine;
 
using UnityEngine;
using System.Collections;
+
 
+
public class OnCollideSound : MonoBehaviour  
public class OnCollideSound : MonoBehaviour {
+
 
+
public GameObject collisionSoundPrefab;
+
public float      triggerMagnitude = 2;
+
 
+
void OnCollisionEnter( Collision collision )
+
 
{
 
{
if (collision.relativeVelocity.magnitude > triggerMagnitude)
+
public GameObject collisionSoundPrefab;
Instantiate(collisionSoundPrefab, this.transform.position, this.transform.rotation);
+
public float      triggerMagnitude = 2;
}
+
public bool      attachToMe = true;
 
+
 +
void OnCollisionEnter( Collision collision )
 +
{
 +
if (collision.relativeVelocity.magnitude > triggerMagnitude)
 +
{
 +
GameObject o = (GameObject) Instantiate(collisionSoundPrefab, this.transform.position, this.transform.rotation);
 +
if(attachToMe)
 +
o.transform.parent = transform;
 +
}
 +
}
 
}
 
}
 
</csharp>
 
</csharp>

Revision as of 05:33, 21 September 2008


Author: Opless

Description

Simple Prefab spawning collision, great for collision sounds

CSharp - OnCollideSound.cs

<csharp> using UnityEngine;

public class OnCollideSound : MonoBehaviour { public GameObject collisionSoundPrefab; public float triggerMagnitude = 2; public bool attachToMe = true;

void OnCollisionEnter( Collision collision ) { if (collision.relativeVelocity.magnitude > triggerMagnitude) { GameObject o = (GameObject) Instantiate(collisionSoundPrefab, this.transform.position, this.transform.rotation); if(attachToMe) o.transform.parent = transform; } } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox