playMaker

Author Topic: Position is inside 2D Collider / Position is inside 2D Rigidbody  (Read 1027 times)

10high

  • Playmaker Newbie
  • *
  • Posts: 33
    • Cult Manager - Google Play
Position is inside 2D Collider / Position is inside 2D Rigidbody
« on: November 03, 2021, 09:41:07 AM »
Two custom actions:
1. Tests if a position is inside a 2D collider (with NO 2D Rigidbody)
2. Tests if a position is inside ANY collider attached to a 2D Rigidbody.

I managed to adapt another custom action but my coding skills are extremely limited, so it can probably be done better/more elegantly - if anyone has any suggestions. The actions are pasted below.

These actions test against the actual colliders and not the bounds (which can return a false result, depending on the shape).

They do NOT work with composite colliders. I've tested them with box colliders and polygon colliders, but not with circle or capsule colliders.

Position Inside Collider 2D

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2020. All rights reserved. 
// License: Attribution 4.0 International(CC BY 4.0)

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Physics 2d")]
[Tooltip("Send event and optionally set bool if you are inside or outside a specific 2D collider. Does NOT work if the game object has a rigid body and does NOT work with composite colliders!")]
public class PositionInsideCollider2D : FsmStateAction
{

public FsmObject colliderTarget;
[UIHint(UIHint.Variable)]
public FsmVector2 point;

public FsmBool insideCollider;
public FsmEvent insideEvent;
public FsmEvent outsideEvent;

public override void Reset()
{
colliderTarget = null;
point = null;
insideCollider = null;
insideEvent = null;
outsideEvent = null;
}


public override void OnEnter()
{

Collider2D c = colliderTarget.Value as Collider2D;
Vector2 tempPoint = point.Value;
Vector2 closest = c.ClosestPoint(tempPoint);


if (closest == tempPoint)

{
insideCollider.Value = true;
}

else
{
insideCollider.Value = false;
}


if (insideCollider.Value ==  true)

{
Fsm.Event(insideEvent);
}

if (insideCollider.Value ==  false)

{
Fsm.Event(outsideEvent);
}

else {
Finish();
}
}


}
}

Position Inside Rigidbody 2D

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2020. All rights reserved. 
// License: Attribution 4.0 International(CC BY 4.0)

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Physics 2d")]
[Tooltip("Send event and optionally set bool if you are inside or outside ANY collider attached to 2D rigidbody. Does NOT work with composite colliders!")]
public class PositionInsideRigidbody2D : FsmStateAction
{

public FsmObject rigidbodyTarget;
[UIHint(UIHint.Variable)]
public FsmVector2 point;

public FsmBool insideCollider;
public FsmEvent insideEvent;
public FsmEvent outsideEvent;

public override void Reset()
{
rigidbodyTarget = null;
point = null;
insideCollider = null;
insideEvent = null;
outsideEvent = null;
}


public override void OnEnter()
{

Rigidbody2D rg = rigidbodyTarget.Value as Rigidbody2D;
Vector2 tempPoint = point.Value;
Vector2 closest = rg.ClosestPoint(tempPoint);


if (closest == tempPoint)

{
insideCollider.Value = true;
}

else
{
insideCollider.Value = false;
}


if (insideCollider.Value ==  true)

{
Fsm.Event(insideEvent);
}

if (insideCollider.Value ==  false)

{
Fsm.Event(outsideEvent);
}

else {
Finish();
}
}


}
}
Cult Manager