Skip to content

Commit

Permalink
feat: fish following spline
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoneerC committed Sep 26, 2024
1 parent 198547b commit 7fc8933
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 5 additions & 4 deletions Assets/Scenes/Reparation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -9747,23 +9747,23 @@ 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
value: -1
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
Expand Down Expand Up @@ -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}
Expand Down
16 changes: 10 additions & 6 deletions Assets/Scripts/Creatures/Fish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -35,7 +37,6 @@ private void Start()

// Get the spline from the container
_spline = _splineContainer.Spline;
_previousPosition = transform.position;
}

private void Update()
Expand All @@ -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()
Expand Down

0 comments on commit 7fc8933

Please sign in to comment.