@@ -13,7 +13,9 @@ public class Fish : MonoBehaviour
13
13
private float _progress ; // Progress along the spline (0 to 1)
14
14
private SplineContainer _splineContainer ;
15
15
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
17
19
18
20
private void Start ( )
19
21
{
@@ -35,7 +37,6 @@ private void Start()
35
37
36
38
// Get the spline from the container
37
39
_spline = _splineContainer . Spline ;
38
- _previousPosition = transform . position ;
39
40
}
40
41
41
42
private void Update ( )
@@ -50,18 +51,21 @@ private void Update()
50
51
_progress = loop ? 0f : 1f ; // Loop or clamp progress
51
52
}
52
53
53
- // Get the position and tangent from the spline
54
+ // Get the global position and tangent from the spline
54
55
Vector3 currentPosition = _spline . EvaluatePosition ( _progress ) ;
55
56
Vector3 tangent = _spline . EvaluateTangent ( _progress ) ;
56
57
57
- // Move the fish to the new position along the spline
58
+ // Move the fish to the global spline position
58
59
transform . position = currentPosition ;
59
60
60
61
// Rotate the fish to face the direction of movement (using tangent)
61
62
Quaternion targetRotation = Quaternion . LookRotation ( tangent ) ;
62
- transform . rotation = Quaternion . Slerp ( transform . rotation , targetRotation , rotationSpeed * Time . deltaTime ) ;
63
63
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 ) ;
65
69
}
66
70
67
71
private void OnDrawGizmos ( )
0 commit comments