From 7fc89333a36443d6bad5c2b6c9b740da6343c4ae Mon Sep 17 00:00:00 2001 From: pyoneerC Date: Thu, 26 Sep 2024 09:19:48 -0300 Subject: [PATCH] feat: fish following spline --- Assets/Scenes/Reparation.unity | 9 +++++---- Assets/Scripts/Creatures/Fish.cs | 16 ++++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Assets/Scenes/Reparation.unity b/Assets/Scenes/Reparation.unity index 06d6952..ca499e0 100644 --- a/Assets/Scenes/Reparation.unity +++ b/Assets/Scenes/Reparation.unity @@ -9747,15 +9747,15 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} propertyPath: m_LocalPosition.x - value: -29.205198 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} propertyPath: m_LocalPosition.y - value: 9.15306 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} propertyPath: m_LocalPosition.z - value: -51.334007 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} propertyPath: m_LocalRotation.w @@ -9763,7 +9763,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} propertyPath: m_LocalRotation.x - value: -0 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} propertyPath: m_LocalRotation.y @@ -9838,6 +9838,7 @@ MonoBehaviour: swimSpeed: 3 rotationSpeed: 5 loop: 1 + rotationOffset: {x: 0, y: -90, z: 0} --- !u!4 &509367958 stripped Transform: m_CorrespondingSourceObject: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3} diff --git a/Assets/Scripts/Creatures/Fish.cs b/Assets/Scripts/Creatures/Fish.cs index 8e04c4a..88e02d5 100644 --- a/Assets/Scripts/Creatures/Fish.cs +++ b/Assets/Scripts/Creatures/Fish.cs @@ -13,7 +13,9 @@ public class Fish : MonoBehaviour private float _progress; // Progress along the spline (0 to 1) private SplineContainer _splineContainer; private Spline _spline; - private Vector3 _previousPosition; + + // Offset to correct fish orientation + public Vector3 rotationOffset = new Vector3(0f, -90f, 0f); // 90-degree Y-axis offset to face spline direction correctly private void Start() { @@ -35,7 +37,6 @@ private void Start() // Get the spline from the container _spline = _splineContainer.Spline; - _previousPosition = transform.position; } private void Update() @@ -50,18 +51,21 @@ private void Update() _progress = loop ? 0f : 1f; // Loop or clamp progress } - // Get the position and tangent from the spline + // Get the global position and tangent from the spline Vector3 currentPosition = _spline.EvaluatePosition(_progress); Vector3 tangent = _spline.EvaluateTangent(_progress); - // Move the fish to the new position along the spline + // Move the fish to the global spline position transform.position = currentPosition; // Rotate the fish to face the direction of movement (using tangent) Quaternion targetRotation = Quaternion.LookRotation(tangent); - transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); - _previousPosition = currentPosition; + // Apply additional rotation offset to adjust the fish's orientation + targetRotation *= Quaternion.Euler(rotationOffset); + + // Smoothly rotate the fish to the target rotation + transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); } private void OnDrawGizmos()