Sliding crane can track world targets

This commit is contained in:
nothke 2024-08-17 14:38:36 +02:00
parent 0d1c168736
commit ecf58d32c7
2 changed files with 23 additions and 4 deletions

View File

@ -2354,7 +2354,7 @@ GameObject:
- component: {fileID: 826892524} - component: {fileID: 826892524}
- component: {fileID: 826892523} - component: {fileID: 826892523}
m_Layer: 0 m_Layer: 0
m_Name: Cube m_Name: TGT
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -2440,7 +2440,7 @@ Transform:
m_GameObject: {fileID: 826892522} m_GameObject: {fileID: 826892522}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
@ -2646,6 +2646,8 @@ MonoBehaviour:
yTransform: {fileID: 2110680922} yTransform: {fileID: 2110680922}
yRange: 40 yRange: 40
xRange: 4.5 xRange: 4.5
testTgt: {fileID: 826892526}
target: {x: 0, y: 0, z: 0}
--- !u!114 &992787539 stripped --- !u!114 &992787539 stripped
MonoBehaviour: MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114565867274256808, guid: d295472d4ce25404e9aed2627c34801f, type: 3} m_CorrespondingSourceObject: {fileID: 114565867274256808, guid: d295472d4ce25404e9aed2627c34801f, type: 3}

View File

@ -14,19 +14,36 @@ public class SlidingCrane : MonoBehaviour
public float yRange = 10; public float yRange = 10;
public float xRange = 5; public float xRange = 5;
public Transform testTgt;
public Vector3 target;
void Start() void Start()
{ {
yMotion.AccelerateTo(1); yMotion.AccelerateTo(1);
xMotion.AccelerateTo(1); xMotion.AccelerateTo(1);
} }
// Update is called once per frame
void Update() void Update()
{ {
target = testTgt.position;
Vector3 localTarget = transform.InverseTransformPoint(target);
xMotion.Update(Time.deltaTime); xMotion.Update(Time.deltaTime);
yMotion.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); 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);
} }
} }