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 position of the target object to the mover object.
|
|
|
|
* If `IsLocal` is true, uses local position instead of global position.
|
|
|
|
*/
|
2020-01-31 02:37:27 +00:00
|
|
|
public class CopyPositionConstraint : IConstraint
|
|
|
|
{
|
|
|
|
private readonly string moversName;
|
|
|
|
private readonly string targetName;
|
|
|
|
|
|
|
|
private readonly bool local = false;
|
|
|
|
|
|
|
|
// Cached components
|
|
|
|
private readonly Transform mover;
|
|
|
|
private readonly Transform target;
|
|
|
|
|
|
|
|
public CopyPositionConstraint(ConfigNode node, Part p)
|
|
|
|
{
|
2020-01-31 06:24:39 +00:00
|
|
|
node.TryGetValue("moversName", ref moversName);
|
2020-01-31 02:37:27 +00:00
|
|
|
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
|
|
|
|
|
|
|
mover = p.FindModelTransform(moversName);
|
|
|
|
target = p.FindModelTransform(targetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
{
|
|
|
|
if (mover == null || target == null) return;
|
|
|
|
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
mover.localPosition = target.localPosition;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mover.position = target.position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|