Skip to content

Commit

Permalink
feat: fish spline
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoneerC committed Sep 25, 2024
1 parent 1ec50f5 commit 669b1ad
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 40 deletions.
234 changes: 229 additions & 5 deletions Assets/Scenes/Reparation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -9207,11 +9207,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 77df7a834c991ce4c966b3383d967636, type: 3}
m_Name:
m_EditorClassIdentifier:
swimSpeed: 15
changeDirectionInterval: 10
swimHeight: 0.5
swimFrequency: 20
randomWanderAmount: 0.5
splineObject: {fileID: 557190188}
swimSpeed: 3
rotationSpeed: 5
loop: 1
--- !u!4 &509367958 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 5146681718063874739, guid: 0cd2fb479650c304eac7efd1ad26408e, type: 3}
Expand Down Expand Up @@ -10550,6 +10549,198 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 550523076}
m_CullTransparentMesh: 1
--- !u!1 &557190188
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 557190189}
- component: {fileID: 557190190}
m_Layer: 0
m_Name: Spline 1
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &557190189
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 557190188}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1495273419}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &557190190
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 557190188}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dab5c7d4c32e743048dfca98e2d5914f, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Spline:
m_EditModeType: 1
m_Knots: []
m_MetaData: []
m_Closed: 0
m_IntData:
m_Data: []
m_FloatData:
m_Data: []
m_Float4Data:
m_Data: []
m_ObjectData:
m_Data: []
m_Splines:
- m_EditModeType: 1
m_Knots:
- Position:
x: 4.6
y: -15
z: -64.5
TangentIn:
x: 0
y: 0
z: -1
TangentOut:
x: 0
y: 0
z: 1
Rotation:
value:
x: 0
y: 0
z: 0
w: 1
- Position:
x: 30
y: -10
z: -74.8
TangentIn:
x: 0
y: 0
z: -1
TangentOut:
x: 0
y: 0
z: 1
Rotation:
value:
x: 0
y: 0
z: 0
w: 1
- Position:
x: 47.62
y: -20
z: -13.3
TangentIn:
x: 0
y: 0
z: -1
TangentOut:
x: 0
y: 0
z: 1
Rotation:
value:
x: 0
y: 0
z: 0
w: 1
- Position:
x: 23.9
y: -30
z: -8.26
TangentIn:
x: 0
y: 0
z: -1
TangentOut:
x: 0
y: 0
z: 1
Rotation:
value:
x: 0
y: 0
z: 0
w: 1
- Position:
x: 8.9
y: -14.97
z: -6.5
TangentIn:
x: 0
y: 0
z: -1
TangentOut:
x: 0
y: 0
z: 1
Rotation:
value:
x: 0
y: 0
z: 0
w: 1
- Position:
x: -5.6
y: -17
z: 12
TangentIn:
x: 0
y: 0
z: -1
TangentOut:
x: 0
y: 0
z: 1
Rotation:
value:
x: 0
y: 0
z: 0
w: 1
m_MetaData:
- Mode: 2
Tension: 0.5
- Mode: 2
Tension: 0.5
- Mode: 2
Tension: 0.5
- Mode: 2
Tension: 0.5
- Mode: 2
Tension: 0.5
- Mode: 2
Tension: 0.5
m_Closed: 1
m_IntData:
m_Data: []
m_FloatData:
m_Data: []
m_Float4Data:
m_Data: []
m_ObjectData:
m_Data: []
m_Knots:
m_KnotsLink: []
--- !u!4 &557404567 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: 0dc7ed63429e1b045bdec55b2f62133f, type: 3}
Expand Down Expand Up @@ -30841,6 +31032,38 @@ Transform:
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: 445f30229dfa5814182e1cf3f70a53aa, type: 3}
m_PrefabInstance: {fileID: 1935490292}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1495273417
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1495273419}
m_Layer: 0
m_Name: ----------- Fish Splines ----------------------------------
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1495273419
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1495273417}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -15.590047, y: 5.8634176, z: 9.638606}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 557190189}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1497520578
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -44522,3 +44745,4 @@ SceneRoots:
- {fileID: 1743274444}
- {fileID: 2129618630}
- {fileID: 2033713812}
- {fileID: 1495273419}
88 changes: 53 additions & 35 deletions Assets/Scripts/Creatures/Fish.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,79 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;

public class Fish : MonoBehaviour
{
public float swimSpeed = 2f;
public float changeDirectionInterval = 2f;
public float swimHeight = 0.5f;
public float swimFrequency = 1f;
public float randomWanderAmount = 0.5f;
public GameObject splineObject; // Reference to the GameObject that holds the SplineContainer
public float swimSpeed = 1f; // Speed along the spline
public float rotationSpeed = 2f; // Speed of rotation to face the spline direction
public bool loop = true; // Should the fish loop back to the start of the spline?

private Vector2 _direction;
private float _timer;
private float _initialY;
private float _progress; // Progress along the spline (0 to 1)
private SplineContainer _splineContainer;
private Spline _spline;
private Vector3 _previousPosition;

private void Start()
{
ChangeDirection();
_timer = changeDirectionInterval;
_initialY = transform.position.y;
// Check if splineObject is assigned, then get the SplineContainer component
if (splineObject != null)
{
_splineContainer = splineObject.GetComponent<SplineContainer>();
if (_splineContainer == null)
{
Debug.LogError("No SplineContainer found on the referenced GameObject.");
return;
}
}
else
{
Debug.LogError("No GameObject with a SplineContainer assigned to Fish.");
return;
}

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

private void Update()
{
transform.Translate(_direction * (swimSpeed * Time.deltaTime));
float newY = _initialY + Mathf.Sin(Time.time * swimFrequency) * swimHeight;
transform.position = new Vector3(transform.position.x, newY, transform.position.z);
if (_spline == null) return;

// Move fish along the spline
_progress += swimSpeed * Time.deltaTime / _spline.GetLength(); // Normalize speed across spline length

_timer -= Time.deltaTime;
if (_timer <= 0f)
if (_progress > 1f)
{
ChangeDirection();
_timer = changeDirectionInterval;
_progress = loop ? 0f : 1f; // Loop or clamp progress
}

_direction += new Vector2(Random.Range(-randomWanderAmount, randomWanderAmount), Random.Range(-randomWanderAmount, randomWanderAmount));
_direction.Normalize();
// Get the position and tangent from the spline
Vector3 currentPosition = _spline.EvaluatePosition(_progress);
Vector3 tangent = _spline.EvaluateTangent(_progress);

Vector2 position = transform.position;
if (position.x < -5 || position.x > 5 || position.y < -5 || position.y > 5)
{
_direction = -_direction;
}
}
// Move the fish to the new position along the spline
transform.position = currentPosition;

private void ChangeDirection()
{
float angle = Random.Range(0f, 360f);
_direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)).normalized;
// 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;
}

private void OnTriggerEnter2D(Collider2D other)
private void OnDrawGizmos()
{
if (other.CompareTag("Player"))
// Draw the spline path in the editor (optional)
if (_splineContainer == null || _splineContainer.Spline == null) return;
Gizmos.color = Color.cyan;
Spline spline = _splineContainer.Spline;
for (float t = 0; t < 1; t += 0.01f)
{
Vector2 fleeDirection = (transform.position - other.transform.position).normalized;
_direction = fleeDirection;
Vector3 point = spline.EvaluatePosition(t);
Gizmos.DrawSphere(point, 0.05f);
}
}
}
}
1 change: 1 addition & 0 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"com.unity.searcher": "4.9.2",
"com.unity.settings-manager": "2.0.1",
"com.unity.shadergraph": "14.0.11",
"com.unity.splines": "2.6.1",
"com.unity.test-framework": "1.1.33",
"com.unity.testtools.codecoverage": "1.2.5",
"com.unity.textmeshpro": "3.0.6",
Expand Down
Loading

0 comments on commit 669b1ad

Please sign in to comment.