using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// A Projectile that spawns an explosion effect on death and causes the player to see a damage effect when hit. /// Inherits from Projectile.cs, where most logic not specific to the ZED Drone Battle sample is handled. /// public class LaserShot_Drone : ZEDProjectile { /// /// Explosion object to spawn on death. /// [Tooltip("Explosion object to spawn on death.")] public GameObject explosionPrefab; /// /// The current distance the object is from the left eye camera, used for collision calculation. /// private float camdistance; private void Update() { camdistance = Vector3.Distance(transform.position, cam.transform.position); if (camdistance < 1.5f) speed = 1f; else speed = 16f; } /// /// Called when the projectile hits the real world. /// As the real world can't be a distinct gameobject (for now) we just spawn FX and destroy the projectile. /// /// /// public override void OnHitRealWorld(Vector3 collisionpoint, Vector3 collisionnormal) { SpawnExplosion(collisionpoint, collisionnormal); Destroy(gameObject); } /// /// Called when the projectile hits a virtual object. /// If that object has a PlayerDamageReceiver, deal damage and destroy the projectile. /// Otherwise ignore it so the drone can't shoot itself. /// /// public override void OnHitVirtualWorld(RaycastHit hitinfo) { //Check to see if it's the player we hit. If so, deal damage. PlayerDamageReceiver receiver = hitinfo.collider.gameObject.GetComponent(); if (receiver != null) //We don't want any collision logic if it's not hitting the player, so we can avoid the drone shooting itself. { receiver.TakeDamage(); Destroy(gameObject); } } /// /// Creates an explosion effect at the desired location, used when the laser is destroyed. /// /// Where to spawn the explosion. /// Which direction to orient the explosion. Should be based off the surface normal of the object the projectile hit. void SpawnExplosion(Vector3 position, Vector3 normal) { if (explosionPrefab) //Only spawn an explosion if we have a prefab to spawn { GameObject explosion = Instantiate(explosionPrefab); explosion.transform.position = position; explosion.transform.rotation = Quaternion.LookRotation(normal); } } }