90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BehaviorDesigner.Runtime.Tasks.Movement
|
|
{
|
|
[HelpURLAttribute("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=18")]
|
|
[TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}WithinDistanceIcon.png")]
|
|
[TaskDescription("Check to see if the any object specified by the object list or tag is within the distance specified of the current agent.")]
|
|
[TaskCategory("Movement")]
|
|
public class WithinDistance : Conditional
|
|
{
|
|
[Tooltip("Should the 2D version be used?")]
|
|
public bool usePhysics2D;
|
|
|
|
[Tooltip("The distance that the object needs to be within")]
|
|
public SharedFloat magnitude;
|
|
|
|
[Tooltip("If true, the object must be within line of sight to be within distance. For example, if this option is enabled then an object behind a wall will not be within distance even though it may be physically close to the other object")]
|
|
public SharedBool lineOfSight;
|
|
|
|
[Tooltip("The LayerMask of the objects to ignore when performing the line of sight check")]
|
|
public LayerMask ignoreLayerMask = 1 << LayerMask.NameToLayer("Ignore Raycast");
|
|
|
|
[Tooltip("The offset relative to the pivot position")]
|
|
public SharedVector3 offset;
|
|
|
|
[Tooltip("The target offset relative to the pivot position")]
|
|
public SharedVector3 targetOffset;
|
|
|
|
[Tooltip("An array of objects to check to see if they are within distance")]
|
|
public SharedGameObjectList objects;
|
|
|
|
[Tooltip("If the object list is null then find the potential objects based off of the tag")]
|
|
public SharedString objectTag;
|
|
|
|
[Tooltip("The object variable that will be set when a object is found what the object is")]
|
|
public SharedGameObject returnedObject;
|
|
|
|
private float sqrMagnitude;
|
|
|
|
public override void OnAwake()
|
|
{
|
|
sqrMagnitude = magnitude.Value * magnitude.Value;
|
|
}
|
|
|
|
public override void OnStart()
|
|
{
|
|
if (!string.IsNullOrEmpty(objectTag.Value))
|
|
{
|
|
GameObject[] array = GameObject.FindGameObjectsWithTag(objectTag.Value);
|
|
objects.Value = new List<GameObject>();
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
objects.Value.Add(array[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public override TaskStatus OnUpdate()
|
|
{
|
|
if (transform == null || objects.Value == null)
|
|
{
|
|
return TaskStatus.Failure;
|
|
}
|
|
for (int i = 0; i < objects.Value.Count; i++)
|
|
{
|
|
Vector3 vector = objects.Value[i].transform.position - (transform.position + offset.Value);
|
|
if (Vector3.SqrMagnitude(vector) < sqrMagnitude)
|
|
{
|
|
if (!lineOfSight.Value)
|
|
{
|
|
returnedObject.Value = objects.Value[i];
|
|
return TaskStatus.Success;
|
|
}
|
|
if ((bool)MovementUtility.LineOfSight(transform, offset.Value, objects.Value[i], targetOffset.Value, usePhysics2D, ignoreLayerMask.value))
|
|
{
|
|
returnedObject.Value = objects.Value[i];
|
|
return TaskStatus.Success;
|
|
}
|
|
}
|
|
}
|
|
return TaskStatus.Failure;
|
|
}
|
|
|
|
public override void OnDrawGizmos()
|
|
{
|
|
}
|
|
}
|
|
}
|