using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// A Projectile that spawns an explosion effect on death and deals damage to objects with ILaserable in their root gameobject.
/// Inherits from Projectile.cs, where most logic not specific to the ZED Drone Battle sample is handled.
///
public class LaserShot_Player : ZEDProjectile
{
///
/// How much damage we do to a drone, or another object that inherits from ILaserable.
///
[Tooltip("How much damage we do to a drone, or another object that inherits from ILaserable.")]
public int Damage = 10;
///
/// Explosion object to spawn on death.
///
[Tooltip("Explosion object to spawn on death.")]
public GameObject explosionPrefab;
///
/// 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 the object has a component that implements ILaserable, deal damage and destroy the projectile.
/// Otherwise ignore it so the player can't shoot themselves.
///
///
public override void OnHitVirtualWorld(RaycastHit hitinfo)
{
//Deal damage to an object if it contains ILaserable, such as a drone
ILaserable laserable = hitinfo.collider.transform.root.GetComponent();
if (laserable != null) //We don't want any collision logic if it doesn't have ILaserable, so we can avoid the player shooting themselves in the head.
{
//Inflict damage on the object we hit
laserable.TakeDamage(Damage);
//Create the explosion
SpawnExplosion(hitinfo.point, hitinfo.normal);
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);
}
}
}