2020-01-31 02:37:27 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Restock.Constraints
|
|
|
|
{
|
|
|
|
[System.Serializable]
|
2020-01-31 06:24:39 +00:00
|
|
|
/**
|
|
|
|
* Copies the rotation of the target object to the rotator object.
|
|
|
|
* If `IsLocal` is true, uses local rotation instead of global rotation.
|
|
|
|
*/
|
2020-01-31 02:37:27 +00:00
|
|
|
public class CopyRotationConstraint : IConstraint
|
|
|
|
{
|
|
|
|
private readonly string rotatorsName;
|
|
|
|
private readonly string targetName;
|
|
|
|
|
|
|
|
private readonly bool local = false;
|
|
|
|
|
|
|
|
// 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 CopyRotationConstraint(ConfigNode node, Part p)
|
|
|
|
{
|
|
|
|
node.TryGetValue("rotatorsName", ref rotatorsName);
|
|
|
|
node.TryGetValue("targetName", ref targetName);
|
2020-01-31 06:24:39 +00:00
|
|
|
node.TryGetValue("isLocal", ref local);
|
2020-01-31 02:37:27 +00:00
|
|
|
|
|
|
|
rotator = p.FindModelTransform(rotatorsName);
|
|
|
|
target = p.FindModelTransform(targetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
{
|
|
|
|
if (rotator == null || target == null) return;
|
|
|
|
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
rotator.localRotation = target.localRotation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rotator.rotation = target.rotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|