| | | 1 | | using UnityEngine; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | public class WaterCollision : MonoBehaviour |
| | | 5 | | { |
| | | 6 | | public ParticleSystem splashPrefab; |
| | | 7 | | private ParticleSystem splash; |
| | | 8 | | private ParticleSystem pSystem; |
| | | 9 | | private List<ParticleCollisionEvent> collisionEvents; |
| | | 10 | | private ParticleCollisionEvent collisionEvent; |
| | | 11 | | |
| | | 12 | | void Awake() |
| | 1 | 13 | | { |
| | | 14 | | // Create instance of splash |
| | 1 | 15 | | this.splash = GameObject.Instantiate(this.splashPrefab); |
| | 1 | 16 | | this.splash.Stop(); |
| | | 17 | | // Get particle system and initialize list for later |
| | 1 | 18 | | this.pSystem = this.GetComponent<ParticleSystem>(); |
| | 1 | 19 | | this.collisionEvents = new List<ParticleCollisionEvent>(); |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | void OnParticleCollision(GameObject other) |
| | 3178 | 23 | | { |
| | | 24 | | // Get collision events |
| | 3178 | 25 | | int numCollisionEvents = this.pSystem.GetCollisionEvents(other, this.collisionEvents); |
| | | 26 | | // Get component for water interaction |
| | 3178 | 27 | | IWaterInteractable go = other.gameObject.GetComponent<IWaterInteractable>(); |
| | | 28 | | // If the component allows for interaction and there are collisions, send the event |
| | 3178 | 29 | | if (numCollisionEvents > 0) |
| | 3178 | 30 | | { |
| | 3178 | 31 | | this.collisionEvent = this.collisionEvents[0]; |
| | | 32 | | // Only send water hit if there's a gameobject |
| | 3178 | 33 | | if (go != null) |
| | 0 | 34 | | { |
| | 0 | 35 | | go.WaterHit(collisionEvent.normal); |
| | 0 | 36 | | } |
| | 3178 | 37 | | this.HandleSplash(collisionEvent.intersection, collisionEvent.normal); |
| | 3178 | 38 | | } |
| | 3178 | 39 | | } |
| | | 40 | | |
| | | 41 | | void HandleSplash(Vector3 position, Vector3 normal) |
| | 3178 | 42 | | { |
| | 3178 | 43 | | this.splash.gameObject.transform.position = position; |
| | 3178 | 44 | | this.splash.gameObject.transform.up = normal; |
| | 3178 | 45 | | this.splash.Play(); |
| | 3178 | 46 | | } |
| | | 47 | | } |