SHARE
TWEET

ImpactCollision.cs

a guest Jul 10th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class ImpactCollision : MonoBehaviour
  4. {
  5.     public GameObject collisionEffect;
  6.     public GameObject destroyedVersion;
  7.  
  8.     public float rotationSpeed = 100f;
  9.     public float minimumEffectForce = 2f;
  10.     public float minimumBreakForce = 4f;
  11.  
  12.     private GameObject instantiatedEffect;
  13.     private ParticleSystem activeParticleSystem;
  14.     private bool effectActive = false;
  15.  
  16.     private void OnCollisionEnter(Collision collision)
  17.     {
  18.         if (collision.relativeVelocity.magnitude > minimumEffectForce && !effectActive)
  19.         {
  20.             instantiatedEffect = Instantiate(collisionEffect, transform.position, Quaternion.identity);
  21.             activeParticleSystem = instantiatedEffect.GetComponent<ParticleSystem>();
  22.             activeParticleSystem.Play();
  23.             effectActive = true;
  24.  
  25.             if (collision.relativeVelocity.magnitude > minimumBreakForce)
  26.             {
  27.                 Instantiate(destroyedVersion, transform.position, transform.rotation);
  28.                 Destroy(gameObject);
  29.             }
  30.         }
  31.     }
  32.  
  33.     private void Update()
  34.     {
  35.         if (!effectActive) return;
  36.  
  37.         if (instantiatedEffect.transform.position != transform.position)
  38.         {
  39.             instantiatedEffect.transform.position = Vector3.MoveTowards(instantiatedEffect.transform.position,
  40.                 transform.position,
  41.                 2f * Time.deltaTime);
  42.         }
  43.  
  44.         instantiatedEffect.transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
  45.         if (activeParticleSystem.isStopped)
  46.         {
  47.             Destroy(instantiatedEffect);
  48.             effectActive = false;
  49.         }
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top