BITFALL/Assets/BITKit/UnityPluginsSupport/NodeCanvas/LookTargetSmooth.cs

73 lines
1.9 KiB
C#

using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using UnityEngine.AI;
namespace BITKit.Node {
[Category("AI")]
public class LookTargetSmooth : ActionTask<NavMeshAgent>
{
public BBParameter<Transform> target;
public BBParameter<bool> repeat = false;
private Transform transform;
private bool initialUpdateRotation;
//Use for initialization. This is called only once in the lifetime of the task.
//Return null if init was successfull. Return an error string otherwise
protected override string OnInit() {
transform = agent.transform;
initialUpdateRotation = agent.updateRotation;
return null;
}
public override void OnDrawGizmosSelected()
{
if (target.isNoneOrNull) return;
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, target.value.position);
}
protected override string info
{
get
{
if(!transform|| !target.value) return "No target";
return "Not Initialized";
}
}
//This is called once each time the task is enabled.
//Call EndAction() to mark the action as finished, either in success or failure.
//EndAction can be called from anywhere.
protected override void OnExecute()
{
agent.updateRotation = false;
}
//Called once per frame while the action is active.
protected override void OnUpdate() {
if (target.isNoneOrNull) return;
var _dir = target.value.position - transform.position;
var direction =Quaternion.LookRotation(Vector3.ProjectOnPlane(_dir ,Vector3.up)) ;
transform.rotation = Quaternion.RotateTowards(transform.rotation,direction,Time.deltaTime*agent.angularSpeed);
if (repeat.value is false && transform.rotation == direction)
{
EndAction(true);
}
}
//Called when the task is disabled.
protected override void OnStop() {
agent.updateRotation = initialUpdateRotation;
}
//Called when the task is paused.
protected override void OnPause() {
}
}
}