Range Trigger on non-dialog system code?

Announcements, support questions, and discussion for the Dialogue System.
terrymorgan
Posts: 97
Joined: Wed Sep 10, 2014 5:29 pm

Range Trigger on non-dialog system code?

Post by terrymorgan »

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));
}
}
}

}

}
User avatar
Tony Li
Posts: 22129
Joined: Thu Jul 18, 2013 1:27 pm

Re: Range Trigger on non-dialog system code?

Post by Tony Li »

Range Trigger is a generally handy script, not just for the Dialogue System. But since it has an optional Condition, you can also configure it to conditionally work only if certain quests are active, certain Lua variables are set, etc.

p.s. - While it's generally not a good idea to post an Asset Store product's source code online, Range Trigger is so peripherally related to the Dialogue System that's its fine in this case. If you edit your post, highlight the source code, and click the [ Code ] button above, it will add BBCodes to format it as code. This makes it easier for people to select and copy.
terrymorgan
Posts: 97
Joined: Wed Sep 10, 2014 5:29 pm

Re: Range Trigger on non-dialog system code?

Post by terrymorgan »

While it's generally not a good idea to post an Asset Store product's source code online
Oops, my bad, I'll see if it works, you only explained it in the email. I searched your docs for it but didn't find anything about it.
User avatar
Tony Li
Posts: 22129
Joined: Thu Jul 18, 2013 1:27 pm

Re: Range Trigger on non-dialog system code?

Post by Tony Li »

No worries. You'll probably want to set Condition > Accepted Tags to "Player" so it only enables and disables the roam script when the player enters and exits the trigger.
terrymorgan
Posts: 97
Joined: Wed Sep 10, 2014 5:29 pm

Re: Range Trigger on non-dialog system code?

Post by terrymorgan »

'Add a Range Trigger and a trigger collider (e.g., a Sphere Collider with radius 20, Is Trigger ticked) to your random roamer. Then assign the random roam script to the Range Trigger's Components list. You can put the Range Trigger and Trigger Collider on a child GameObject if you like.'

#1 I can only drag the ai_randomroam script into 'components' if it's in the same area, not as a child GO.


It doesn't seem to have any effect.

If I just run the game, in scene view he walks around for 10 seconds and disables himself. I put the 'accepted tag's to Player and when I play the game the player crosses collider- spider disappears (after I've checked him in the inspector). Screenshot is before I added
accepted tags entry.

Base spider no range trigger 836k
https://dl.dropboxusercontent.com/u/102 ... itypackage
Attachments
range_trigger.jpg
range_trigger.jpg (36.99 KiB) Viewed 2790 times
User avatar
Tony Li
Posts: 22129
Joined: Thu Jul 18, 2013 1:27 pm

Re: Range Trigger on non-dialog system code?

Post by Tony Li »

Hi Terry,

Try these settings:
  • Condition > Accepted Tags > Size: 1
  • Condition > Accepted Tags > Element 0: Player
  • Game Objects > Size: 0
  • Components > Size: 1
  • Components > Element 0: spider_etc (AI_RandomRoam)
Don't assign the spider to the GameObjects list, since this will deactivate the entire GameObject. You only want to assign it to the Components list to disable the roam component.
terrymorgan wrote:#1 I can only drag the ai_randomroam script into 'components' if it's in the same area, not as a child GO.
1. Open another Inspector view so you have two open.

2. Inspect the Range Trigger, and click the padlock icon in the upper right of one of the inspectors. This will lock it on that GameObject.

3. Inspect the other GameObject (parent/child/whatever) that contains the AI_RandomRoam script. Drag the AI_RandomRoam script in that inspector to the Components field of the inspector that's padlocked.
terrymorgan
Posts: 97
Joined: Wed Sep 10, 2014 5:29 pm

Re: Range Trigger on non-dialog system code?

Post by terrymorgan »

Spider starts out moving

Spider with the Tony trigger, also threw in tmpxyz's version of ai_randomroam.js

'AI_RandomRoad.js used a coroutine to navigate the agent, so disabling it wont stop it moving.'

'You could try this one, it will stop the coroutine once disabled.'

https://dl.dropboxusercontent.com/u/102 ... itypackage
836k
I left out the RPG player for simplicity, the spider should start out stopped anyway.


I also tried this with RPG kit's icode version of trigger, didn't work.
User avatar
Tony Li
Posts: 22129
Joined: Thu Jul 18, 2013 1:27 pm

Re: Range Trigger on non-dialog system code?

Post by Tony Li »

Does it work?
terrymorgan
Posts: 97
Joined: Wed Sep 10, 2014 5:29 pm

Re: Range Trigger on non-dialog system code?

Post by terrymorgan »

Spider starts out moving
Of course not, he shouldn't move until his range is entered.
User avatar
Tony Li
Posts: 22129
Joined: Thu Jul 18, 2013 1:27 pm

Re: Range Trigger on non-dialog system code?

Post by Tony Li »

terrymorgan wrote:Of course not, he shouldn't move until his range is entered.
The Range Trigger only acts when the player enters or exits the trigger collider. Since the player hasn't entered or exited the collider, the roam script is left in its starting state. If you want the spider to start out not moving, disable the roam script at design time. The Range Trigger will then activate it as soon as the player enters the trigger collider.
Post Reply