ReStocked/Source/Restock/Constraints/LookAtConstraint.cs

43 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using UnityEngine;
namespace Restock.Constraints
{
[System.Serializable]
/**
* Points the rotator object at the target object
*/
public class LookAtConstraint : IConstraint
{
private readonly string rotatorsName;
private readonly string targetName;
// Cached components
private readonly Transform rotator;
private readonly Transform target;
public LookAtConstraint(ConfigNode node, Part p)
{
node.TryGetValue("rotatorsName", ref rotatorsName);
node.TryGetValue("targetName", ref targetName);
rotator = p.FindModelTransform(rotatorsName);
if (rotator == null)
{
throw new Exception($"Missing rotator transform {rotator}");
}
target = p.FindModelTransform(targetName);
if (target == null)
{
throw new Exception($"Missing target transform {target}");
}
}
public void Update()
{
var lookPos = target.position - rotator.position;
var rotation = Quaternion.LookRotation(lookPos, rotator.up);
rotator.rotation = rotation;
}
}
}