summaryrefslogtreecommitdiff
path: root/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-08-31 19:07:21 +0800
committerchai <chaifix@163.com>2021-08-31 19:07:21 +0800
commitffd1d5af496e0a0eff343b27c4f0f965bbbf79eb (patch)
treec5a8ae2116040ce8c483454b2e82093f247db341 /Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs
parent476abf41a7357db0dd870f0d221f1a26481ab2bf (diff)
*projectile
Diffstat (limited to 'Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs')
-rw-r--r--Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs142
1 files changed, 142 insertions, 0 deletions
diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs
new file mode 100644
index 00000000..ee62553b
--- /dev/null
+++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs
@@ -0,0 +1,142 @@
+using UnityEngine;
+using System.Collections;
+
+/* THIS CODE IS JUST FOR PREVIEW AND TESTING */
+// Feel free to use any code and picking on it, I cannot guaratnee it will fit into your project
+public class ExplodingProjectile : MonoBehaviour
+{
+ public GameObject impactPrefab;
+ public GameObject explosionPrefab;
+ public float thrust;
+
+ public Rigidbody thisRigidbody;
+
+ public GameObject particleKillGroup;
+ private Collider thisCollider;
+
+ public bool LookRotation = true;
+ public bool Missile = false;
+ public Transform missileTarget;
+ public float projectileSpeed;
+ public float projectileSpeedMultiplier;
+
+ public bool ignorePrevRotation = false;
+
+ public bool explodeOnTimer = false;
+ public float explosionTimer;
+ float timer;
+
+ private Vector3 previousPosition;
+
+ // Use this for initialization
+ void Start()
+ {
+ thisRigidbody = GetComponent<Rigidbody>();
+ if (Missile)
+ {
+ missileTarget = GameObject.FindWithTag("Target").transform;
+ }
+ thisCollider = GetComponent<Collider>();
+ previousPosition = transform.position;
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+ /* if(Input.GetButtonUp("Fire2"))
+ {
+ Explode();
+ }*/
+ timer += Time.deltaTime;
+ if (timer >= explosionTimer && explodeOnTimer == true)
+ {
+ Explode();
+ }
+
+ }
+
+ void FixedUpdate()
+ {
+ if (Missile)
+ {
+ projectileSpeed += projectileSpeed * projectileSpeedMultiplier;
+ // transform.position = Vector3.MoveTowards(transform.position, missileTarget.transform.position, 0);
+
+ transform.LookAt(missileTarget);
+
+ thisRigidbody.AddForce(transform.forward * projectileSpeed);
+ }
+
+ if (LookRotation && timer >= 0.05f)
+ {
+ transform.rotation = Quaternion.LookRotation(thisRigidbody.velocity);
+ }
+
+ CheckCollision(previousPosition);
+
+ previousPosition = transform.position;
+ }
+
+ void CheckCollision(Vector3 prevPos)
+ {
+ RaycastHit hit;
+ Vector3 direction = transform.position - prevPos;
+ Ray ray = new Ray(prevPos, direction);
+ float dist = Vector3.Distance(transform.position, prevPos);
+ if (Physics.Raycast(ray, out hit, dist))
+ {
+ transform.position = hit.point;
+ Quaternion rot = Quaternion.FromToRotation(Vector3.forward, hit.normal);
+ Vector3 pos = hit.point;
+ Instantiate(impactPrefab, pos, rot);
+ if (!explodeOnTimer && Missile == false)
+ {
+ Destroy(gameObject);
+ }
+ else if (Missile == true)
+ {
+ thisCollider.enabled = false;
+ particleKillGroup.SetActive(false);
+ thisRigidbody.velocity = Vector3.zero;
+ Destroy(gameObject, 5);
+ }
+
+ }
+ }
+
+ void OnCollisionEnter(Collision collision)
+ {
+ if (collision.gameObject.tag != "FX")
+ {
+ ContactPoint contact = collision.contacts[0];
+ Quaternion rot = Quaternion.FromToRotation(Vector3.forward, contact.normal);
+ if (ignorePrevRotation)
+ {
+ rot = Quaternion.Euler(0, 0, 0);
+ }
+ Vector3 pos = contact.point;
+ Instantiate(impactPrefab, pos, rot);
+ if (!explodeOnTimer && Missile == false)
+ {
+ Destroy(gameObject);
+ }
+ else if (Missile == true)
+ {
+
+ thisCollider.enabled = false;
+ particleKillGroup.SetActive(false);
+ thisRigidbody.velocity = Vector3.zero;
+
+ Destroy(gameObject, 5);
+
+ }
+ }
+ }
+
+ void Explode()
+ {
+ Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.Euler(0, 0, 0));
+ Destroy(gameObject);
+ }
+
+} \ No newline at end of file