using UnityEngine;
public class ProjectilePickup : MonoBehaviour
{
// Spawns a Pickup object at the location of the projectile.
[Tooltip("Collider ItemPickup will use as a trigger, can be a non trigger collider by default.")]
[SerializeField] GameObject c_Pickup;
[Tooltip("Should the pickup objects rotation be set to the rotation of the projectile object?")]
[SerializeField] bool b_CopyRotation;
[Tooltip("Destroy the projectile gameObject after the pickup object is spawned?")]
[SerializeField] bool b_DestroyOnSpawn;
[Tooltip("Delay destruction time after pickup is spawned. Only used if Destroy On Spawn is true.")]
[SerializeField] float f_DestroyDelay = 0;
public void SpawnPickupDelayed(float delay)
{
// Run the SpawnPickup method after a given delay
// The delay can be helpful in ensuring the projectile gets parented to the gameObject that it stuck to before spawning the pickup object.
Invoke("SpawnPickup", delay);
}
public void SpawnPickup()
{
GameObject parent = gameObject.transform.parent.gameObject;
int parentLayer = parent.layer;
// If parentLayer is of SubCharacter then we assume the projectile is attached to a character and we do not spawn an arrow pickup
if (parentLayer == 30) { return; }
// We cannot spawn the pickup object if its null
if (c_Pickup == null) { return; }
Quaternion rotation = Quaternion.identity;
Vector3 position = gameObject.transform.position;
// If b_CopyRotation, then set the rotation Quaternion to the projectiles current rotation.
if (b_CopyRotation) { rotation = gameObject.transform.rotation; }
// Create the ItemPickup
Instantiate(c_Pickup, position, rotation, parent.transform);
// If b_DestroyOnSpawn is false then we want to keep the original projectile alive.
if (!b_DestroyOnSpawn) { return; }
// Destroy the projectile after a given delay.
Destroy(gameObject, f_DestroyDelay);
}
}