Page 1 of 2

Range Trigger on non-dialog system code?

Posted: Tue Apr 19, 2016 1:30 pm
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));
}
}
}

}

}

Re: Range Trigger on non-dialog system code?

Posted: Tue Apr 19, 2016 2:10 pm
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.

Re: Range Trigger on non-dialog system code?

Posted: Wed Apr 20, 2016 6:32 pm
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.

Re: Range Trigger on non-dialog system code?

Posted: Wed Apr 20, 2016 7:37 pm
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.

Re: Range Trigger on non-dialog system code?

Posted: Sat Apr 23, 2016 10:16 am
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

Re: Range Trigger on non-dialog system code?

Posted: Sat Apr 23, 2016 10:37 am
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.

Re: Range Trigger on non-dialog system code?

Posted: Sun Apr 24, 2016 12:50 pm
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.

Re: Range Trigger on non-dialog system code?

Posted: Sun Apr 24, 2016 2:19 pm
by Tony Li
Does it work?

Re: Range Trigger on non-dialog system code?

Posted: Mon Apr 25, 2016 7:41 pm
by terrymorgan
Spider starts out moving
Of course not, he shouldn't move until his range is entered.

Re: Range Trigger on non-dialog system code?

Posted: Mon Apr 25, 2016 8:16 pm
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.