0
Answered

Triggers in a 2D game

Oliver Marshall 2 weeks ago in Game Creator updated by Marti (Lead Developer) 1 day ago 10

Hi all

What's the best way to handle triggers in a 2d top down environment? I note that the trigger prompts me to add one of several colliders but they are all 3D colliders. If I add a 2D collider then the game works but I get errors in the console and the trigger never works. 

Is there another way I should be doing this? 

I should point out that the object with a script, and box collider 2D as a trigger itself works, separate to GC, it's just the GC trigger which doesn't trigger.

Olly

Unity version:
2019.2.13
Game Creator version:
1.07

Answer

Answer
Answered

Hi Oliver!

Not sure if we answered you back at Discord. But Game Creator only supports 3D collisions. We could easily add support for 2D collisions (adding Triggers that detect 2D Physics) but that would only add partial support and might be confusing to some users.

The good news is that you can easily create your own custom Triggers to support 2D colliders. It's basically copying and pasting the scripts "IgniterOnEnter" and "IgniterOnExit" and replace the "OnCollisionEnter" line for "OnCollision2DEnter.

If you have problems doing so, feel free to drop us a line :-)

PS: We're investigating adding support for 2D out of the box for the next Game Creator version.

Its also worth saying that my Player has the Hook Player attached. 

I think the issue just comes down to not being able to add a 2D collider to the GC event. 

Answer
Answered

Hi Oliver!

Not sure if we answered you back at Discord. But Game Creator only supports 3D collisions. We could easily add support for 2D collisions (adding Triggers that detect 2D Physics) but that would only add partial support and might be confusing to some users.

The good news is that you can easily create your own custom Triggers to support 2D colliders. It's basically copying and pasting the scripts "IgniterOnEnter" and "IgniterOnExit" and replace the "OnCollisionEnter" line for "OnCollision2DEnter.

If you have problems doing so, feel free to drop us a line :-)

PS: We're investigating adding support for 2D out of the box for the next Game Creator version.

+1

Thanks Marti. 

Yes, I got that working this morning doing exactly that. I now have a 2D trigger with the following code:

namespace GameCreator.Core
{
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using GameCreator.Variables;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif

    [AddComponentMenu("")]
    public class IgniterTriggerEnter2D : Igniter 
    {
        public Collider2D otherCollider;

        #if UNITY_EDITOR
        public new static string NAME = "Object/On Trigger Enter 2D";
        public new static string COMMENT = "Leave empty to trigger any object";
//        public new static bool REQUIRES_COLLIDER = true;
        #endif

        [Space][VariableFilter(Variable.DataType.GameObject)]
        public VariableProperty storeSelf = new VariableProperty(Variable.VarType.GlobalVariable);

        [Space][VariableFilter(Variable.DataType.GameObject)]
        public VariableProperty storeCollider = new VariableProperty(Variable.VarType.GlobalVariable);

        private void OnTriggerEnter2D(Collider2D c)
        {
            this.storeSelf.Set(gameObject);
            this.storeCollider.Set(c.gameObject);

            if (this.otherCollider == null)
            {
                this.ExecuteTrigger(c.gameObject);
            }
            else if (this.otherCollider.gameObject.GetInstanceID() == c.gameObject.GetInstanceID())
            {
                this.ExecuteTrigger(c.gameObject);
            }
        }
    }
}


I've basically changed the Collider to Collider2D and removed the lines about rigidbodies and the REQUIRES_COLLIDER.

I would say that I think *some* support of 2D is required. Even if it's just having a REQUIRED_COLLIDER2D option which prompts the user to add a 2D collider, something which you can put in to custom Triggers, that would help. 

I now have another issue with my player not appearing where he's meant to on new scenes, but that's for another day. 

Thanks


Olly

Excellent! We'll see what we can do about this :-)

PS: You must add the Player character in every scene. The Player is not carried when loading other scenes, as the "Load Scene" action simply loads a new one.

Thanks. 

So what does "Load Scene with Player" actually do? It doesn't seem to move the player TO the scene, or move the player WITHIN the scene. Poor chap always appears in the place I've put the prefab.

It actually moves the player within the scene. Let's say you're in SceneA and you use the "Load Scene with Player" Action in order to load SceneB with the Player Position property at (5,5,5).

This will first load SceneB and then teleport the Player (which should exist in that scene) at position (5,5,5). Do you have a different outcome?

Thanks Marti

What happens here is that the scene transition happens but the player doesn't move to the new co-ordinates.

Olly

This is the action. Moving to -8.28,3.82

But on the new scene, the player appears, but at the co-ords I placed the prefab at. In this case 1.19,1.79

Under review

I haven't experienced this issue, and might be a bug. I'll open a ticket on our system to have a loot at it. Could you tell us whether you're using the Save Game or Load Game action before? These might have a say in this issue.