[Diving] Movement with a rock and 3 ropes is working
This commit is contained in:
38
Assets/Scripts/RockFollower.cs
Normal file
38
Assets/Scripts/RockFollower.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class RockFollower : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
public Transform bottleTransform; // Assign the bottle's transform in inspector or via script
|
||||
public WobbleBehavior bottleWobble; // Assign if you want to use vertical offset
|
||||
|
||||
[Header("Movement Settings")]
|
||||
public float followStiffness = 4f; // Higher = stiffer, lower = more responsive
|
||||
public bool useWobbleOffset = true; // Should the rock follow the bottle's vertical wobble?
|
||||
public float baseY = -6f; // The base Y position for the rock
|
||||
|
||||
private float velocityX; // For SmoothDamp
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (bottleTransform == null) return;
|
||||
|
||||
// Target horizontal position is bottle's X
|
||||
float targetX = bottleTransform.position.x;
|
||||
float currentX = transform.position.x;
|
||||
|
||||
// Smoothly follow bottle's X with stiffer motion
|
||||
float newX = Mathf.SmoothDamp(currentX, targetX, ref velocityX, 1f / followStiffness);
|
||||
|
||||
// Calculate Y position
|
||||
float newY = baseY;
|
||||
if (useWobbleOffset && bottleWobble != null)
|
||||
{
|
||||
newY += bottleWobble.VerticalOffset;
|
||||
}
|
||||
|
||||
// Set new position (only X and Y, keep Z)
|
||||
transform.position = new Vector3(newX, newY, transform.position.z);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user