Help me understand the colliders

tieup

New member
Messages
4
Reactions
0
Points
1
Hi! I'm trying to learn about plugins in VAM. I don't have Unity background, but was able to google most of the things.
What I want to achieve:
1) Create CollisionTrigger atom
2) Listen to collision on this new atom
3) Display debug message if a collision happens

My code is below. The collider is created and I can select it in the scene. However, I can't trigger any action, so (3) doesn't work.
May be I miss some silly detail? I'm trying to trigger the collision by moving a person or a toy object. Am I doing something wrong?
Thank you for your help!

C#:
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;

namespace TieupNNJ {

    public class ColliderTest: MVRScript {

        public override void Init()
        {
            pluginLabelJSON.val = "Collider test";
            StartCoroutine(CreateCollistionTrigger());
        }

        private IEnumerator CreateCollistionTrigger()
        {
            yield return SuperController.singleton.AddAtomByType("CollisionTrigger", "MyCollider");
            try {
                Atom createdCollider = SuperController.singleton.GetAtomByUid("MyCollider");
                createdCollider.gameObject.AddComponent<CollisionTracker>();
                SuperController.LogMessage("Collider created");
            } catch (Exception ex) {
                SuperController.LogError(ex.ToString());
            }
        }


        public void OnDestroy()
        {
            
        }

        public void OnUnLoad()
        {
            
        }

        // Runs once when plugin loads - after Init()
        protected void Start()
        {
            // show a message
            SuperController.LogMessage(pluginLabelJSON.val + " Loaded");
        }

        // A Unity thing - runs every physics cycle
        public void FixedUpdate()
        {
            // put code here
        }

        // Unity thing - runs every rendered frame
        public void Update()
        {


        }
    }

    class CollisionTracker : MonoBehaviour
    {
        private void OnTriggerEnter(Collider other)
        {
            // This will be called when another collider enters the trigger
            SuperController.LogMessage(other.name + " entered the trigger.");
        }

        private void OnTriggerStay(Collider other)
        {
            // This is called once per frame for every collider that stays inside the trigger
            // Use with caution as it can be performance intensive if many objects are inside the trigger
        }

        private void OnTriggerExit(Collider other)
        {
            // This will be called when a collider exits the trigger
            Debug.Log(other.name + " exited the trigger.");
        }
    }

}
 
Since you're adding a collision trigger atom, I assume your goal is to detect collisions in the same way that VAM detects collisions for the purpose of triggering the collision trigger actions? If so, you could just add a trigger action (start, transition or end) that latches onto a storable parameter in your plugin, and let VAM handle the actual collision detection.

A simple way to add a trigger action via plugin:
C#:
using SimpleJSON;

JSONStorableAction _onCollisionStartAction;

public override void Init()
{
    _onCollisionStartAction = new JSONStorableAction("OnCollisionStartAction", OnCollisionStart);
    RegisterAction(_onCollisionStartAction);
}

private void OnCollisionStart() {
   // do stuff
}

// call this when the collision trigger atom is added
private void AddTrigger(Atom atom)
{
    JSONStorable triggerHandler = atom.GetStorableByID("Trigger"); // assuming atom is a CollisionTrigger atom
    JSONClass handlerJSON = triggerHandler.GetJSON(true, true, true).AsObject;

    JSONClass triggerJSON = new JSONClass
    {
        ["receiverAtom"] = containingAtom.uid,
        ["receiver"] = this.storeId,
        ["receiverTargetName"] = _onCollisionStartAction.name,
    };

    JSONArray startActions = triggerJson["startActions"].AsArray;
    startActions.Add(triggerJSON);

    triggerHandler.LateRestoreFromJSON(handlerJSON);
}

You can do similar things for other triggerable parameters (floats, bools etc), and for transition and end actions.
 
Thank
Since you're adding a collision trigger atom, I assume your goal is to detect collisions in the same way that VAM detects collisions for the purpose of triggering the collision trigger actions? If so, you could just add a trigger action (start, transition or end) that latches onto a storable parameter in your plugin, and let VAM handle the actual collision detection.

A simple way to add a trigger action via plugin:
C#:
using SimpleJSON;

JSONStorableAction _onCollisionStartAction;

public override void Init()
{
    _onCollisionStartAction = new JSONStorableAction("OnCollisionStartAction", OnCollisionStart);
    RegisterAction(_onCollisionStartAction);
}

private void OnCollisionStart() {
   // do stuff
}

// call this when the collision trigger atom is added
private void AddTrigger(Atom atom)
{
    JSONStorable triggerHandler = atom.GetStorableByID("Trigger"); // assuming atom is a CollisionTrigger atom
    JSONClass handlerJSON = triggerHandler.GetJSON(true, true, true).AsObject;

    JSONClass triggerJSON = new JSONClass
    {
        ["receiverAtom"] = containingAtom.uid,
        ["receiver"] = this.storeId,
        ["receiverTargetName"] = _onCollisionStartAction.name,
    };

    JSONArray startActions = triggerJson["startActions"].AsArray;
    startActions.Add(triggerJSON);

    triggerHandler.LateRestoreFromJSON(handlerJSON);
}

You can do similar things for other triggerable parameters (floats, bools etc), and for transition and end actions.
Thank you so much for your reply!
I don't really plan to allow to change action from the plugin UI. I just want to do some calculations and morph changes using the code.
Any suggestions where to look for an example?
 
Thank

Thank you so much for your reply!
I don't really plan to allow to change action from the plugin UI. I just want to do some calculations and morph changes using the code.
Any suggestions where to look for an example?
Even so, you can use the above method to let VAM handle the collision detection for you. It's just taking advantage of the existing implementation VAM has for doing things in response to detecting collisions. It's kind of taking a roundabout via the VAM trigger system and back into your code, so it's not as elegant or simple as detecting and reacting to collisions directly, but saves you having to add your own collision handler component.

If you want to do a custom solution, the CollisionTracker class you have in your original post looks like a good starting point. A couple things:
  • instead of OnTriggerEnter, OnTriggerStay and OnTriggerExit, use OnCollisionEnter, OnCollisionStay and OnCollisionExit. The former only detect collisions with colliders that are set as "trigger" colliders, which isn't the case with any of the usual colliders in VAM. See https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
  • You need to add that component to the gameObject that you want to detect collisions on... I would try atom.transform.gameObject.
Every collision trigger (whether on the Collision Trigger atom or on other atoms) in VAM contains a CollisionTriggerEventHandler component which implements those OnCollisionEnter etc. methods. You should check out that class within the VAM assembly, it might give you some ideas. You can browse the code by opening VaM_Data/Managed/Assembly-CSharp.dll with a C# decompiler. Good luck!
 
Even so, you can use the above method to let VAM handle the collision detection for you. It's just taking advantage of the existing implementation VAM has for doing things in response to detecting collisions. It's kind of taking a roundabout via the VAM trigger system and back into your code, so it's not as elegant or simple as detecting and reacting to collisions directly, but saves you having to add your own collision handler component.

If you want to do a custom solution, the CollisionTracker class you have in your original post looks like a good starting point. A couple things:
  • instead of OnTriggerEnter, OnTriggerStay and OnTriggerExit, use OnCollisionEnter, OnCollisionStay and OnCollisionExit. The former only detect collisions with colliders that are set as "trigger" colliders, which isn't the case with any of the usual colliders in VAM. See https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
  • You need to add that component to the gameObject that you want to detect collisions on... I would try atom.transform.gameObject.
Every collision trigger (whether on the Collision Trigger atom or on other atoms) in VAM contains a CollisionTriggerEventHandler component which implements those OnCollisionEnter etc. methods. You should check out that class within the VAM assembly, it might give you some ideas. You can browse the code by opening VaM_Data/Managed/Assembly-CSharp.dll with a C# decompiler. Good luck!
Thank you very much for your reply!
 
Back
Top Bottom