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