Search Unity

Visual Scripting: On Collision Enter 2D v On Trigger Enter 2D

Discussion in 'Editor & General Support' started by SurreyMuso, May 4, 2021.

  1. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    63
    I'm exploring Visual Scripting as a way of teaching a prototyping class to Undergraduate Students.

    Player character dies: When I use On Collision Enter 2D, I can apply the Game Object Get Layer Unit directly on the Collider output. There is no need to use the Component Get Game Object Unit first.

    Player character collects coins: However, when I use the On Trigger Enter 2D Unit, I have to use the Component Get Game Object Unit before I can use the Get Layer Unit.

    I would have thought that these two ought to work in the same way - and preferably exposing the Game Object so that the additional step of Get Game Object is not required.

    Is this an inconsistency in the these two Units or is there some rationale behind it?

    Many thanks...
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    What is a "Get Layer Unit"?
     
  3. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    63
    It's the Unit that takes a Game Object and extracts the number representing the Layer from the collection of layers. It doesn't return the layer name but rather its index. You then need to use the "Layer to Name" Unit to match against Layer names.

    Screenshot 2021-05-04 at 20.15.15.png
     
    Last edited: May 4, 2021
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Oh sorry I somehow missed that this is a question about Visual Scripting.

    I suspect your confusion is this though:

    OnTriggerEnter2D gives you a Collider2D parameter, as shown in your screenshot.

    OnCollisionEnter2D gives you a Collision2D parameter. That's a different object entirely. That object has a
    gameObject
    property on it, from which you can then get the
    layer
    .

    See:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
    https://docs.unity3d.com/ScriptReference/Collision2D.html
    https://docs.unity3d.com/ScriptReference/Collider2D.html
     
    SurreyMuso likes this.
  5. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    63
    No - my fault for posting in the wrong forum. Should have been in VS.

    Thank you. While I realise the difference between collision (physics/forces) and triggers (entering an area), I was confused by two things:

    (1) being able to apply Get Game Object on the On Collision Unit, despite its being unnecessary, and
    (2) the output port on both is marked "Collider", when - as you point out - they are not both Collider2D objects.

    Perhaps the output port literal for OnTrigger2D might be changed...