Adventure Creator Wikia

The following custom Action is used to check which side an object is on relative to another. To install it, see the Manual's "Custom Actions" chapter or follow the steps in this tutorial.

ActionCheckSide.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

[System.Serializable]
public class ActionCheckSide : ActionCheck
{

[SerializeField] private bool otherIsPlayer;
[SerializeField] private Transform otherTransform;
[SerializeField] private int otherTransformParameterID = -1;
[SerializeField] private int otherTransformConstantID = 0;

[SerializeField] private bool isPlayer;
[SerializeField] private Transform transform;
[SerializeField] private int transformParameterID = -1;
[SerializeField] private int transformConstantID = 0;

[SerializeField] private SideType sideType = SideType.Forward;
private enum SideType { Forward, Up, Right };


public ActionCheckSide ()
{
this.isDisplayed = true;
category = ActionCategory.Custom;
title = "Check side";
description = "Checks which side an object is on.";
}


override public void AssignValues (List<ActionParameter> parameters)
{
otherTransform = (otherIsPlayer) ? KickStarter.player.transform : AssignFile (parameters, otherTransformParameterID, otherTransformConstantID, otherTransform);
transform = (isPlayer) ? KickStarter.player.transform : AssignFile (parameters, transformParameterID, transformConstantID, transform);
}


override public bool CheckCondition ()
{
Vector3 relativePosition = transform.position - otherTransform.position;
float dotProduct = 0f;

switch (sideType)
{
case SideType.Forward:
dotProduct = Vector3.Dot (transform.forward, relativePosition);
break;

case SideType.Right:
dotProduct = Vector3.Dot (transform.right, relativePosition);
break;

case SideType.Up:
dotProduct = Vector3.Dot (transform.up, relativePosition);
break;
}
return (dotProduct > 0f);
}


#if UNITY_EDITOR

override public void ShowGUI (List<ActionParameter> parameters)
{
EditorGUILayout.BeginVertical (CustomStyles.thinBox);
EditorGUILayout.LabelField ("Main object", EditorStyles.boldLabel);
isPlayer = EditorGUILayout.Toggle ("Is player?", isPlayer);

if (!isPlayer)
{
transformParameterID = Action.ChooseParameterGUI ("Transform:", parameters, transformParameterID, ParameterType.GameObject);
if (transformParameterID >= 0)
{
transformConstantID = 0;
transform = null;
}
else
{
transform = (Transform) EditorGUILayout.ObjectField ("Transform:", transform, typeof (Transform), true);

transformConstantID = FieldToID (transform, transformConstantID);
transform = IDToField (transform, transformConstantID, false);
}
}

sideType = (SideType) EditorGUILayout.EnumPopup ("Side to check:", sideType);

EditorGUILayout.EndVertical ();

EditorGUILayout.BeginVertical (CustomStyles.thinBox);
EditorGUILayout.LabelField ("Other object", EditorStyles.boldLabel);
otherIsPlayer = EditorGUILayout.Toggle ("Is player?", otherIsPlayer);

if (!otherIsPlayer)
{
otherTransformParameterID = Action.ChooseParameterGUI ("Transform:", parameters, otherTransformParameterID, ParameterType.GameObject);
if (otherTransformParameterID >= 0)
{
otherTransformConstantID = 0;
otherTransform = null;
}
else
{
otherTransform = (Transform) EditorGUILayout.ObjectField ("Transform:", otherTransform, typeof (Transform), true);

otherTransformConstantID = FieldToID (transform, otherTransformConstantID);
otherTransform = IDToField (otherTransform, otherTransformConstantID, false);
}
}

EditorGUILayout.EndVertical ();
}


override public void AssignConstantIDs (bool saveScriptsToo = false, bool fromAssetFile = false)
{
AssignConstantID (transform, transformConstantID, transformParameterID);
AssignConstantID (otherTransform, otherTransformConstantID, otherTransformParameterID);
}

#endif

}

}