From ecf58d32c71f4e8a983aa8884fd4b2089ed8a44b Mon Sep 17 00:00:00 2001 From: nothke Date: Sat, 17 Aug 2024 14:38:36 +0200 Subject: [PATCH] Sliding crane can track world targets --- .../Scenes/test_conveyor_belt_character.unity | 6 ++++-- Assets/Scripts/SlidingCrane.cs | 21 +++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Assets/Scenes/test_conveyor_belt_character.unity b/Assets/Scenes/test_conveyor_belt_character.unity index 4f26162..356d9fc 100644 --- a/Assets/Scenes/test_conveyor_belt_character.unity +++ b/Assets/Scenes/test_conveyor_belt_character.unity @@ -2354,7 +2354,7 @@ GameObject: - component: {fileID: 826892524} - component: {fileID: 826892523} m_Layer: 0 - m_Name: Cube + m_Name: TGT m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -2440,7 +2440,7 @@ Transform: m_GameObject: {fileID: 826892522} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 21.567997, y: 0.16, z: 24.309193} + m_LocalPosition: {x: 18.43, y: 0.16, z: 59.1} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -2646,6 +2646,8 @@ MonoBehaviour: yTransform: {fileID: 2110680922} yRange: 40 xRange: 4.5 + testTgt: {fileID: 826892526} + target: {x: 0, y: 0, z: 0} --- !u!114 &992787539 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 114565867274256808, guid: d295472d4ce25404e9aed2627c34801f, type: 3} diff --git a/Assets/Scripts/SlidingCrane.cs b/Assets/Scripts/SlidingCrane.cs index 5c0d786..4bed34a 100644 --- a/Assets/Scripts/SlidingCrane.cs +++ b/Assets/Scripts/SlidingCrane.cs @@ -14,19 +14,36 @@ public class SlidingCrane : MonoBehaviour public float yRange = 10; public float xRange = 5; + public Transform testTgt; + public Vector3 target; + void Start() { yMotion.AccelerateTo(1); xMotion.AccelerateTo(1); } - // Update is called once per frame void Update() { + target = testTgt.position; + + Vector3 localTarget = transform.InverseTransformPoint(target); xMotion.Update(Time.deltaTime); yMotion.Update(Time.deltaTime); + Vector2 targetPlanar = new Vector2(localTarget.x, localTarget.z); + + float xTgt = Mathf.InverseLerp(-xRange, xRange, targetPlanar.x); + Debug.Log(xTgt); + 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; + yTransform.localPosition = new Vector3(0, 0, yMotion.progress * yRange); - xTransform.localPosition = new Vector3(xMotion.progress * xRange, 0, 0); + xTransform.localPosition = new Vector3(x, 0, 0); } }