FANDOM


Note: This script is now deprecated with AC v1.67, which exposes a "Detection method" field in 2D and 3D Triggers.

This script provides a 2D Trigger that detects objects by their GameObject position, rather than the presence of a 2D Collider. This means that it can work with characters that have no 2D Rigidbody or 2D Collider components.

To use it:

  1. Paste the code below into a C# script named PointTrigger2D.cs
  2. Attach the script as a component to a GameObject in the scene
  3. Attach a 2D collider (PolygonCollider2D, BoxCollider2D or CircleCollider) to the same GameObject
  4. Check Is Trigger on the collider component
  5. Configure the Point Trigger 2D component's Inspector. You can choose which objects it detects, including the default Player, and if it detects objects entering or leaving it
  6. Assign a Cutscene it should run when triggered
using UnityEngine;
using System.Collections;

namespace AC
{

public class PointTrigger2D : MonoBehaviour
{

#region Variables

[SerializeField] private bool detectsPlayer = true;
[SerializeField] private GameObject[] obsToDetect = null;
[SerializeField] private bool cancelInteractions = false;
[SerializeField] private TriggerReacts triggerReacts = TriggerReacts.OnlyDuringGameplay;

private enum TriggerType { OnEnter=0, Continous=1, OnExit=2 };
[SerializeField] private TriggerType triggerType;

[SerializeField] private Cutscene cutsceneToRun;
[SerializeField] private bool setFirstParameterAsObject;

private Collider2D _collider2D;
private bool[] lastFrameWithins;

#endregion


#region UnityStandards

private void Awake ()
{
_collider2D = GetComponent <Collider2D>();
lastFrameWithins = (detectsPlayer) ? new bool[obsToDetect.Length + 1] : new bool[obsToDetect.Length];
}


private void Update ()
{
if (_collider2D != null)
{
for (int i=0; i<obsToDetect.Length; i++)
{
ProcessObject (obsToDetect[i], i);
}

if (detectsPlayer && KickStarter.player != null)
{
ProcessObject (KickStarter.player.gameObject, lastFrameWithins.Length - 1);
}
}
}

#endregion


#region PrivateFunctions

private void ProcessObject (GameObject objectToCheck, int i)
{
if (objectToCheck != null)
{
bool isInside = CheckForPoint (objectToCheck.transform.position);
if (DetermineValidity (isInside, i))
{
Interact (objectToCheck);
}
}
}


private bool DetermineValidity (bool thisFrameWithin, int i)
{
bool isValid = false;

switch (triggerType)
{
case TriggerType.OnEnter:
if (thisFrameWithin && !lastFrameWithins[i])
{
isValid = true;
}
break;

case TriggerType.Continous:
isValid = thisFrameWithin;
break;

case TriggerType.OnExit:
if (!thisFrameWithin && lastFrameWithins[i])
{
isValid = true;
}
break;

default:
break;
}

lastFrameWithins[i] = thisFrameWithin;
return isValid;
}


private bool CheckForPoint (Vector3 position)
{
return _collider2D.OverlapPoint (position);
}


private void Interact (GameObject collisionOb)
{
if (triggerReacts == TriggerReacts.OnlyDuringGameplay && KickStarter.stateHandler.gameState != GameState.Normal)
{
return;
}
else if (triggerReacts == TriggerReacts.OnlyDuringCutscenes && KickStarter.stateHandler.IsInGameplay ())
{
return;
}

if (cancelInteractions)
{
KickStarter.playerInteraction.StopMovingToHotspot ();
}

if (cutsceneToRun == null)
{
return;
}

if (cutsceneToRun.actionListType == ActionListType.PauseGameplay)
{
KickStarter.playerInteraction.DeselectHotspot (false);
}

// Set correct parameter
if (setFirstParameterAsObject && cutsceneToRun.useParameters && cutsceneToRun.parameters != null && cutsceneToRun.parameters.Count >= 1)
{
if (cutsceneToRun.parameters[0].parameterType == ParameterType.GameObject)
{
cutsceneToRun.parameters[0].gameObject = collisionOb;
}
else
{
ACDebug.Log ("Cannot set the value of parameter 0 ('" + cutsceneToRun.parameters[0].label + "') as it is not of the type 'Game Object'.", this);
}
}

cutsceneToRun.Interact ();
}

#endregion


}

}
Community content is available under CC-BY-SA unless otherwise noted.