OnCollideExplode

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(New page: Category: MonoBehaviour Category: CSharp Category: ReallySimpleScripts Author: Opless == Description == Borrowed from the documentation, if we collide with anything, we spaw...)
 
m
Line 1: Line 1:
 
[[Category: MonoBehaviour]]
 
[[Category: MonoBehaviour]]
[[Category: CSharp]]
+
[[Category: C Sharp]]
 
[[Category: ReallySimpleScripts]]
 
[[Category: ReallySimpleScripts]]
  

Revision as of 13:54, 8 November 2007


Author: Opless

Description

Borrowed from the documentation, if we collide with anything, we spawn an explosion prefab.

Useful for missiles, grenades, etc

CSharp - OnCollideExplode.cs

<csharp> using UnityEngine; using System.Collections;

public class OnCollideExplode : MonoBehaviour {

// A grenade // - instantiates a explosion prefab when hitting a surface // - then destroys itself public GameObject explosionPrefab; public float explodeSecs = -1;

void Awake() { if(explodeSecs > -1) Invoke ("DestroyNow", explodeSecs); }

void OnCollisionEnter( Collision collision ) { // Rotate the object so that the y-axis faces along the normal of the surface ContactPoint contact = collision.contacts[0]; Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal); Vector3 pos = contact.point; Instantiate(explosionPrefab, pos, rot); // Destroy the projectile Destroy (gameObject); }

void DestroyNow() { Instantiate(explosionPrefab, transform.position, transform.rotation); Destroy (gameObject); }



} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox