ReStocked/Source/Restock/Constraints/LookAtConstraint.cs
drewcassidy 85ab757271
Code cleanup
• Pretty up some code for consistency
• Add documentation to constraint modules
• Fix key name for copy position constraint
2020-01-30 22:24:39 -08:00

36 lines
1.0 KiB
C#

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);
target = p.FindModelTransform(targetName);
}
public void Update()
{
if (rotator == null || target == null) return;
var lookPos = target.position - rotator.position;
var rotation = Quaternion.LookRotation(lookPos, target.up);
rotator.rotation = rotation;
}
}
}