Quantcast
Channel: Answers by "dubbreak"
Viewing all articles
Browse latest Browse all 68

Answer by dubbreak

$
0
0
**Step 1.** Use c# so you can define events (JS can only subscribe to event IIRC). **Step 2.** Create an event in your bullet class. public class MuhBullet : MonoBehaviour { public string BulletId; public event Action BulletCollided; //event using the default Action delegate public Delegate void CollisionWithDataDelegate(Collision collision, string name) public event CollisionWithDataDelegate BulletDataCollision; //example with custom delegate void OnCollisionEnter(Collision collision) { if (BulletCollided!=null) BulletCollided(); //Raise event, if subscribers if (BulletDataCollision != null) BulletDataCollision(collision, BulletId); //Raise a different one that has data, if subscribers } } **Step 3.** Subscribe to event. bullet = GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation); bullet.BulletId = "Bill"; bullet.BulletCollided += DoSomethingOnBulletCollision; bullet.BulletDataCollision += DoSomethingWithData; bullet.rigidbody.AddRelativeForce(Vector3(2000,0,0)); //somewhere else public void DoSomethingOnBulletCollision() { Debug.Log("Bullet collided!"); } public void DoSomethingWithData(Collision collision, string name) { Debug.Log("A bullet of the following name collided: " + name); } **Step 4.** Do cool things and be happy.

Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>