Range Trigger on non-dialog system code?
Posted: Tue Apr 19, 2016 1:30 pm
I mailed you this but others may be interested. I'd like to turn off my ai_randomroam animals if the player can't see them (and probably a lot of other stuff like
animations, particle systems, etc.) but I'm stuck with RPG kit which requires a college degree to turn on a light. Ai_randomroam here
https://sites.google.com/site/terrymorg ... randomroam
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Using UnityEngine;
namespace PixelCrushers.DialogueSystem {
/// <summary>
/// This component activates game objects and enables components when it receives
/// OnTriggerEnter and the conditions are true, and deactivates/disables when it
/// receives OnTriggerExit and the conditions are true.
/// </summary>
[AddComponentMenu("Dialogue System/Actor/Range Trigger")]
public class RangeTrigger : MonoBehaviour {
/// <summary>
/// The condition that must be true in order to activate/deactivate target
/// game objects and components when the trigger is entered or exited.
/// </summary>
public Condition condition;
/// <summary>
/// The game objects to affect.
/// </summary>
public GameObject[] gameObjects;
/// <summary>
/// The components to affect.
/// </summary>
public Component[] components;
/// <summary>
/// Activates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The collider that entered the trigger.
/// </param>
public void OnTriggerEnter(Collider other) {
if (condition.IsTrue(other.transform)) SetTargets(true);
}
/// <summary>
/// Deactivates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The collider that exited the trigger.
/// </param>
public void OnTriggerExit(Collider other) {
if (condition.IsTrue(other.transform)) SetTargets(false);
}
/// <summary>
/// Activates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The 2D collider that entered the trigger.
/// </param>
public void OnTriggerEnter2D(Collider2D other) {
if (condition.IsTrue(other.transform)) SetTargets(true);
}
/// <summary>
/// Deactivates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The 2D collider that exited the trigger.
/// </param>
public void OnTriggerExit2D(Collider2D other) {
if (condition.IsTrue(other.transform)) SetTargets(false);
}
/// <summary>
/// Sets the targets active/inactive. Colliders and Renderers aren't MonoBehaviours, so we
/// cast them separately to access their 'enabled' properties.
/// </summary>
/// <param name='value'>
/// <c>true</c> for active, <c>false</c> for inactive.
/// </param>
private void SetTargets(bool value) {
foreach (var gameObject in gameObjects) {
gameObject.SetActive(value);
}
foreach (var component in components) {
if (component is Collider) {
(component as Collider).enabled = value;
} else if (component is Renderer) {
(component as Renderer).enabled = value;
} else if (component is Animation) {
(component as Animation).enabled = value;
} else if (component is Animator) {
(component as Animator).enabled = value;
} else if (component is MonoBehaviour) {
(component as MonoBehaviour).enabled = value;
} else {
if (DialogueDebug.LogWarnings) Debug.LogWarning(string.Format("{0}: Internal error - Range Trigger doesn't know how to handle {1} of type {2}", DialogueDebug.Prefix, component, component.GetType().Name));
}
}
}
}
}
animations, particle systems, etc.) but I'm stuck with RPG kit which requires a college degree to turn on a light. Ai_randomroam here
https://sites.google.com/site/terrymorg ... randomroam
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Using UnityEngine;
namespace PixelCrushers.DialogueSystem {
/// <summary>
/// This component activates game objects and enables components when it receives
/// OnTriggerEnter and the conditions are true, and deactivates/disables when it
/// receives OnTriggerExit and the conditions are true.
/// </summary>
[AddComponentMenu("Dialogue System/Actor/Range Trigger")]
public class RangeTrigger : MonoBehaviour {
/// <summary>
/// The condition that must be true in order to activate/deactivate target
/// game objects and components when the trigger is entered or exited.
/// </summary>
public Condition condition;
/// <summary>
/// The game objects to affect.
/// </summary>
public GameObject[] gameObjects;
/// <summary>
/// The components to affect.
/// </summary>
public Component[] components;
/// <summary>
/// Activates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The collider that entered the trigger.
/// </param>
public void OnTriggerEnter(Collider other) {
if (condition.IsTrue(other.transform)) SetTargets(true);
}
/// <summary>
/// Deactivates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The collider that exited the trigger.
/// </param>
public void OnTriggerExit(Collider other) {
if (condition.IsTrue(other.transform)) SetTargets(false);
}
/// <summary>
/// Activates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The 2D collider that entered the trigger.
/// </param>
public void OnTriggerEnter2D(Collider2D other) {
if (condition.IsTrue(other.transform)) SetTargets(true);
}
/// <summary>
/// Deactivates the target game objects and components if the condition is true.
/// </summary>
/// <param name='other'>
/// The 2D collider that exited the trigger.
/// </param>
public void OnTriggerExit2D(Collider2D other) {
if (condition.IsTrue(other.transform)) SetTargets(false);
}
/// <summary>
/// Sets the targets active/inactive. Colliders and Renderers aren't MonoBehaviours, so we
/// cast them separately to access their 'enabled' properties.
/// </summary>
/// <param name='value'>
/// <c>true</c> for active, <c>false</c> for inactive.
/// </param>
private void SetTargets(bool value) {
foreach (var gameObject in gameObjects) {
gameObject.SetActive(value);
}
foreach (var component in components) {
if (component is Collider) {
(component as Collider).enabled = value;
} else if (component is Renderer) {
(component as Renderer).enabled = value;
} else if (component is Animation) {
(component as Animation).enabled = value;
} else if (component is Animator) {
(component as Animator).enabled = value;
} else if (component is MonoBehaviour) {
(component as MonoBehaviour).enabled = value;
} else {
if (DialogueDebug.LogWarnings) Debug.LogWarning(string.Format("{0}: Internal error - Range Trigger doesn't know how to handle {1} of type {2}", DialogueDebug.Prefix, component, component.GetType().Name));
}
}
}
}
}