using Antares.Vizio.Runtime;
using UnityEngine;
[VisualLogicBlock("Smooth Look At", "Custom Blocks", ParentName = "Camera-Control")]
public class SmoothLookAtBlock : LogicBlock
{
[Parameter(VariableType.In, typeof(Transform), Name = "Target Transform")]
public Variable target;
[Parameter(VariableType.In, typeof(bool), Name = "Smooth")]
public Variable smooth;
[Parameter(VariableType.In, typeof(float), Name = "Damping", DefaultValue = 6f)]
public Variable damping;
[Parameter(VariableType.Out, typeof(string), Name = "Error")]
public Variable error;
[EntryTrigger]
public void In()
{
Transform targ = target.Value as Transform;
if (targ!=null)
{
Transform thisTransform = Component.transform;
if ((bool)smooth.Value)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(targ.position - thisTransform.position);
thisTransform.rotation = Quaternion.Slerp(thisTransform.rotation, rotation, Time.deltaTime * (float)damping.Value);
}
else
{
// Just lookat
thisTransform.LookAt(targ);
}
ActivateTrigger();
}
else
{
error.Value = "Target is NULL";
ActivateTrigger("Error");
}
}
public override void Awake()
{
// Make the rigid body not change rotation
if (Component.rigidbody)
Component.rigidbody.freezeRotation = true;
}
public override void OnInitializeDefaultData()
{
RegisterOutputTrigger("Exit");
RegisterOutputTrigger("Error");
}
}