quality-control/Assets/Scripts/SlidingCrane.cs

96 lines
2.9 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();
2024-08-19 21:01:26 +00:00
public InertialInterpolator heightMotion = InertialInterpolator.Default();
2024-08-17 11:38:26 +00:00
public Transform xTransform;
public Transform yTransform;
public float yRange = 10;
public float xRange = 5;
2024-08-19 21:01:26 +00:00
public float heightRange = 10;
public float heightMax = -0.7f;
public float heightMin = -10;
/// <summary>
/// if targetTrasnform is set, it will go to it. If null it will go to targetPoint
/// </summary>
public Transform targetTransform;
2024-08-17 11:38:26 +00:00
2024-08-19 21:01:26 +00:00
/// <summary>
/// if targetTrasnform is set, it will go to it. If null it will go to targetPoint
/// </summary>
public Vector3 targetPoint;
2024-08-17 12:38:36 +00:00
2024-08-18 14:11:53 +00:00
public Transform cradleTransform;
public Rigidbody cradleRb;
2024-08-20 00:10:02 +00:00
public AudioSource longMotionAudio;
public AudioSource longMotionAudio2;
public float longMotionPitchMult = 1;
public AudioSource sideMotionAudio;
public float sideMotionPitchMult = 1;
public AudioSource heightMotionAudio;
public float heightMotionPitchMult = 1;
2024-08-17 14:29:47 +00:00
private void Start()
{
2024-08-19 21:01:26 +00:00
targetPoint = transform.position;
2024-08-18 14:19:17 +00:00
xMotion.progress = 0.5f;
2024-08-19 21:01:26 +00:00
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.V))
heightMotion.AccelerateTo(0);
if (Input.GetKeyDown(KeyCode.F))
heightMotion.AccelerateTo(1);
2024-08-20 00:10:02 +00:00
longMotionAudio.pitch = yMotion.velocity * longMotionPitchMult;
longMotionAudio2.pitch = yMotion.velocity * (longMotionPitchMult + 0.05f);
sideMotionAudio.pitch = xMotion.velocity * sideMotionPitchMult;
heightMotionAudio.pitch = heightMotion.velocity * heightMotionPitchMult;
2024-08-17 14:29:47 +00:00
}
2024-08-18 14:11:53 +00:00
void FixedUpdate()
2024-08-17 11:38:26 +00:00
{
2024-08-19 21:01:26 +00:00
if (targetTransform)
targetPoint = targetTransform.position;
2024-08-17 12:38:36 +00:00
2024-08-17 14:29:47 +00:00
2024-08-19 21:01:26 +00:00
Vector3 localTarget = transform.InverseTransformPoint(targetPoint);
2024-08-17 14:29:47 +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);
2024-08-18 14:32:52 +00:00
float y = yMotion.progress * yRange - 3;
2024-08-17 12:38:36 +00:00
2024-08-17 14:29:47 +00:00
xMotion.Update(Time.deltaTime);
yMotion.Update(Time.deltaTime);
2024-08-19 21:01:26 +00:00
heightMotion.Update(Time.deltaTime);
2024-08-17 14:29:47 +00:00
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-18 14:11:53 +00:00
2024-08-18 14:19:17 +00:00
//cradleRb.MovePosition(cradleTransform.position);
2024-08-19 21:01:26 +00:00
cradleRb.transform.localPosition = new Vector3(0, Mathf.Lerp(heightMin, heightMax, heightMotion.progress), 0);
2024-08-17 11:38:26 +00:00
}
}