Skip to content

Commit 7fc8933

Browse files
committed
feat: fish following spline
1 parent 198547b commit 7fc8933

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Assets/Scenes/Reparation.unity

+5-4
Original file line numberDiff line numberDiff line change
@@ -9747,23 +9747,23 @@ PrefabInstance:
97479747
objectReference: {fileID: 0}
97489748
- target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
97499749
propertyPath: m_LocalPosition.x
9750-
value: -29.205198
9750+
value: 0
97519751
objectReference: {fileID: 0}
97529752
- target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
97539753
propertyPath: m_LocalPosition.y
9754-
value: 9.15306
9754+
value: 0
97559755
objectReference: {fileID: 0}
97569756
- target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
97579757
propertyPath: m_LocalPosition.z
9758-
value: -51.334007
9758+
value: 0
97599759
objectReference: {fileID: 0}
97609760
- target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
97619761
propertyPath: m_LocalRotation.w
97629762
value: -1
97639763
objectReference: {fileID: 0}
97649764
- target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
97659765
propertyPath: m_LocalRotation.x
9766-
value: -0
9766+
value: 0
97679767
objectReference: {fileID: 0}
97689768
- target: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
97699769
propertyPath: m_LocalRotation.y
@@ -9838,6 +9838,7 @@ MonoBehaviour:
98389838
swimSpeed: 3
98399839
rotationSpeed: 5
98409840
loop: 1
9841+
rotationOffset: {x: 0, y: -90, z: 0}
98419842
--- !u!4 &509367958 stripped
98429843
Transform:
98439844
m_CorrespondingSourceObject: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}

Assets/Scripts/Creatures/Fish.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class Fish : MonoBehaviour
1313
private float _progress; // Progress along the spline (0 to 1)
1414
private SplineContainer _splineContainer;
1515
private Spline _spline;
16-
private Vector3 _previousPosition;
16+
17+
// Offset to correct fish orientation
18+
public Vector3 rotationOffset = new Vector3(0f, -90f, 0f); // 90-degree Y-axis offset to face spline direction correctly
1719

1820
private void Start()
1921
{
@@ -35,7 +37,6 @@ private void Start()
3537

3638
// Get the spline from the container
3739
_spline = _splineContainer.Spline;
38-
_previousPosition = transform.position;
3940
}
4041

4142
private void Update()
@@ -50,18 +51,21 @@ private void Update()
5051
_progress = loop ? 0f : 1f; // Loop or clamp progress
5152
}
5253

53-
// Get the position and tangent from the spline
54+
// Get the global position and tangent from the spline
5455
Vector3 currentPosition = _spline.EvaluatePosition(_progress);
5556
Vector3 tangent = _spline.EvaluateTangent(_progress);
5657

57-
// Move the fish to the new position along the spline
58+
// Move the fish to the global spline position
5859
transform.position = currentPosition;
5960

6061
// Rotate the fish to face the direction of movement (using tangent)
6162
Quaternion targetRotation = Quaternion.LookRotation(tangent);
62-
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
6363

64-
_previousPosition = currentPosition;
64+
// Apply additional rotation offset to adjust the fish's orientation
65+
targetRotation *= Quaternion.Euler(rotationOffset);
66+
67+
// Smoothly rotate the fish to the target rotation
68+
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
6569
}
6670

6771
private void OnDrawGizmos()

0 commit comments

Comments
 (0)