Video Tutorial: How To Use Node Triggers to Detect Changes in Node States
Notice
Subtitles are available for each tutorial in English, Russian, and Chinese.
Warning
The tutorial is created with SDK version 2.18. Not applicable for earlier versions.
Node Trigger is a node without a visual representation that fires callbacks when it is enabled/disabled or its transformation is changed.
The component code displayed in this video is provided below for your convenience:
Trigger.cs
using System.Collections;
using System.Collections.Generic;
using Unigine;
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Trigger : Component
{
public AssetLink explosion;
void position_event_handler(NodeTrigger trigger)
{
Log.Message("Object position has been changed. New position is: {0}\n\n", trigger.WorldPosition.ToString());
}
void enabled_event_handler(NodeTrigger trigger)
{
SoundSource soundSource = new SoundSource(explosion.Path);
soundSource.WorldPosition = trigger.WorldPosition;
soundSource.MaxDistance = 100.0f;
soundSource.Play();
}
NodeTrigger nodeTrigger;
void Init()
{
// Cast Node to NodeTrigger
nodeTrigger = node as NodeTrigger;
if(nodeTrigger){
// Subscribe for Enabled and Position events to perform actions
// when a node is enabled/disabled or changes its position
nodeTrigger.EventEnabled.Connect(enabled_event_handler);
nodeTrigger.EventPosition.Connect(position_event_handler);
}
}
void Update()
{
// Disable the parent object when it is below the plane z = 2.0f
if(node.Parent.WorldPosition.z < 2.0f)
node.Parent.Enabled = false;
}
}
To enable the visualizer and display console messages, add the following code in the Init() method in AppWorldLogic.cpp or any suitable component:
Source code (C#)
// Enable Visualizer
Visualizer.Enabled = true;
// Display console messages on the screen
Unigine.Console.Onscreen = true;
Last update:
2025-03-04
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)