/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Objects { using Opsive.Shared.Game; using Opsive.UltimateCharacterController.SurfaceSystem; using UnityEngine; /// /// The Projectile component moves a Destructible object along the specified path. Can apply damage at the collision point. /// public class Projectile : Destructible { [Tooltip("The length of time the projectile should exist before it deactivates if no collision occurs.")] [SerializeField] protected float m_Lifespan = 10; private ScheduledEventBase m_ScheduledDeactivation; /// /// Initializes the object. This will be called from an object creating the projectile (such as a weapon). /// /// The velocity to apply. /// The torque to apply. /// The amount of damage to apply to the hit object. /// The amount of force to apply to the hit object. /// The number of frames to add the force to. /// The layers that the projectile can impact with. /// The name of the state to activate upon impact. /// The number of seconds until the impact state is disabled. /// A reference to the Surface Impact triggered when the object hits an object. /// The object that instantiated the trajectory object. public override void Initialize(Vector3 velocity, Vector3 torque, float damageAmount, float impactForce, int impactForceFrames, LayerMask impactLayers, string impactStateName, float impactStateDisableTimer, SurfaceImpact surfaceImpact, GameObject originator) { // The projectile can deactivate after it comes in contact with another object or after a specified amount of time. Do the scheduling here to allow // it to activate after a set amount of time. m_ScheduledDeactivation = Scheduler.Schedule(m_Lifespan, Deactivate); base.Initialize(velocity, torque, damageAmount, impactForce, impactForceFrames, impactLayers, impactStateName, impactStateDisableTimer, surfaceImpact, originator); } /// /// The projectile has reached its lifespan. /// private void Deactivate() { OnCollision(null); } /// /// The object has collided with another object. /// /// The RaycastHit of the object. Can be null. protected override void OnCollision(RaycastHit? hit) { if (m_ScheduledDeactivation != null) { Scheduler.Cancel(m_ScheduledDeactivation); m_ScheduledDeactivation = null; } base.OnCollision(hit); } /// /// The component has been disabled. /// protected override void OnDisable() { base.OnDisable(); if (m_ScheduledDeactivation != null) { Scheduler.Cancel(m_ScheduledDeactivation); m_ScheduledDeactivation = null; } } } }