quality-control/Assets/Scripts/SlidingCrane.cs

52 lines
1.3 KiB
C#
Raw Normal View History

2024-08-17 11:38:26 +00:00
using Nothke.Utils;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlidingCrane : MonoBehaviour
{
public InertialInterpolator xMotion = InertialInterpolator.Default();
public InertialInterpolator yMotion = InertialInterpolator.Default();
public Transform xTransform;
public Transform yTransform;
public float yRange = 10;
public float xRange = 5;
2024-08-17 12:38:36 +00:00
public Transform testTgt;
public Vector3 target;
2024-08-17 14:29:47 +00:00
private void Start()
{
target = transform.position;
}
2024-08-17 11:38:26 +00:00
void Update()
{
2024-08-17 14:13:26 +00:00
if (testTgt)
target = testTgt.position;
2024-08-17 12:38:36 +00:00
2024-08-17 14:29:47 +00:00
2024-08-17 12:38:36 +00:00
Vector3 localTarget = transform.InverseTransformPoint(target);
2024-08-17 14:29:47 +00:00
2024-08-17 11:38:26 +00:00
2024-08-17 12:38:36 +00:00
Vector2 targetPlanar = new Vector2(localTarget.x, localTarget.z);
float xTgt = Mathf.InverseLerp(-xRange, xRange, targetPlanar.x);
float yTgt = Mathf.InverseLerp(0, yRange, targetPlanar.y);
xMotion.AccelerateTo(xTgt);
yMotion.AccelerateTo(yTgt);
float x = Mathf.Lerp(-xRange, xRange, xMotion.progress);
float y = yMotion.progress * yRange;
2024-08-17 14:29:47 +00:00
xMotion.Update(Time.deltaTime);
yMotion.Update(Time.deltaTime);
2024-08-17 11:38:26 +00:00
yTransform.localPosition = new Vector3(0, 0, yMotion.progress * yRange);
2024-08-17 12:38:36 +00:00
xTransform.localPosition = new Vector3(x, 0, 0);
2024-08-17 11:38:26 +00:00
}
}