Files
AppleHillsProduction/Assets/Scripts/Movement/RockFollower.cs

47 lines
1.4 KiB
C#

using UnityEngine;
/// <summary>
/// Makes this object follow a target (bottleTransform) horizontally, optionally using a wobble offset for vertical movement.
/// </summary>
public class RockFollower : MonoBehaviour
{
[Header("References")]
public Transform bottleTransform;
public WobbleBehavior bottleWobble;
[Header("Movement Settings")]
public float followStiffness = 4f;
/// <summary>
/// Whether to use the wobble offset for vertical movement.
/// </summary>
public bool useWobbleOffset = true;
/// <summary>
/// The base Y position for the rock.
/// </summary>
public float baseY = -6f;
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);
}
}