57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using System;
|
|
using Core;
|
|
using Unity.Behavior;
|
|
using UnityEngine;
|
|
using Action = Unity.Behavior.Action;
|
|
using Unity.Properties;
|
|
|
|
[Serializable, GeneratePropertyBag]
|
|
[NodeDescription(name: "FindByRaycastAction", story: "Find Player [Target] by raycast from [Self]", category: "Action",
|
|
id: "0211c365022a481bc2c40a1c93ef9f2b")]
|
|
public partial class FindByRaycastAction : Action
|
|
{
|
|
[SerializeReference] public BlackboardVariable<GameObject> Target;
|
|
[SerializeReference] public BlackboardVariable<GameObject> Self;
|
|
[SerializeReference] public BlackboardVariable<float> MaxDistance = new(10.0f);
|
|
[SerializeReference] public BlackboardVariable<Boolean> IsFound = new(false);
|
|
|
|
protected override Status OnStart()
|
|
{
|
|
// initialize the node
|
|
|
|
return Status.Running;
|
|
}
|
|
|
|
protected override Status OnUpdate()
|
|
{
|
|
// will be called every frame until the node returns a status not running
|
|
Target.Value = null;
|
|
IsFound.Value = false;
|
|
|
|
RaycastHit hit;
|
|
if (!Physics.Raycast(Self.Value.transform.position, Self.Value.transform.forward, out hit,
|
|
MaxDistance.Value))
|
|
{
|
|
return Status.Failure;
|
|
}
|
|
// return hit.collider.gameObject == Target.Value ? Status.Success : Status.Failure;
|
|
|
|
PlayerController playerController = hit.collider.gameObject.GetComponent<PlayerController>();
|
|
if (playerController == null)
|
|
{
|
|
return Status.Failure;
|
|
}
|
|
|
|
Target.Value = playerController.gameObject;
|
|
IsFound.Value = true;
|
|
|
|
return Status.Success;
|
|
}
|
|
|
|
protected override void OnEnd()
|
|
{
|
|
// deinitialize the node
|
|
}
|
|
}
|
|
|