2024-06-07 04:57:34 +00:00
|
|
|
using System;
|
2020-01-31 02:37:27 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Restock.Constraints
|
|
|
|
{
|
|
|
|
[System.Serializable]
|
2020-01-31 06:24:39 +00:00
|
|
|
/**
|
|
|
|
* Points the rotator object at the target object
|
|
|
|
*/
|
2020-01-31 02:37:27 +00:00
|
|
|
public class LookAtConstraint : IConstraint
|
|
|
|
{
|
|
|
|
private readonly string rotatorsName;
|
|
|
|
private readonly string targetName;
|
|
|
|
|
|
|
|
// Cached components
|
|
|
|
private readonly Transform rotator;
|
2020-01-31 06:24:39 +00:00
|
|
|
private readonly Transform target;
|
2020-01-31 02:37:27 +00:00
|
|
|
|
|
|
|
public LookAtConstraint(ConfigNode node, Part p)
|
|
|
|
{
|
|
|
|
node.TryGetValue("rotatorsName", ref rotatorsName);
|
|
|
|
node.TryGetValue("targetName", ref targetName);
|
|
|
|
|
|
|
|
rotator = p.FindModelTransform(rotatorsName);
|
2024-06-07 04:57:34 +00:00
|
|
|
if (rotator == null)
|
|
|
|
{
|
|
|
|
throw new Exception($"Missing rotator transform {rotator}");
|
|
|
|
}
|
2020-01-31 02:37:27 +00:00
|
|
|
target = p.FindModelTransform(targetName);
|
2024-06-07 04:57:34 +00:00
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
throw new Exception($"Missing target transform {target}");
|
|
|
|
}
|
2020-01-31 02:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
{
|
|
|
|
var lookPos = target.position - rotator.position;
|
2024-06-07 04:57:34 +00:00
|
|
|
var rotation = Quaternion.LookRotation(lookPos, rotator.up);
|
2020-01-31 02:37:27 +00:00
|
|
|
rotator.rotation = rotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|