Files
AppleHillsProduction/Assets/Scripts/Minigames/DivingForPictures/RopeEndPhysicsFollower.cs
2025-09-21 22:36:05 +02:00

53 lines
1.9 KiB
C#

using UnityEngine;
public class RopeEndPhysicsFollower : MonoBehaviour
{
[Tooltip("Tag of the object this endpoint should follow (e.g., 'player' or 'rock')")]
public string targetTag;
[Tooltip("How quickly the endpoint follows the target")] public float followSpeed = 5f;
[Tooltip("How much trailing (0 = instant, 1 = very slow)")] public float trailing = 0.2f;
[Tooltip("Amplitude of fake gravity/flap")] public float oscillationAmplitude = 0.15f;
[Tooltip("Frequency of fake gravity/flap")] public float oscillationFrequency = 2f;
[Tooltip("If true, disable vertical oscillation (useful for player attachment)")]
public bool disableOscillation = true;
private Transform target;
private Vector3 velocity;
private Vector3 offset;
private float oscillationTime;
void Start()
{
if (!string.IsNullOrEmpty(targetTag))
{
GameObject found = GameObject.FindGameObjectWithTag(targetTag);
if (found) target = found.transform;
}
offset = transform.position - (target ? target.position : Vector3.zero);
oscillationTime = Random.value * Mathf.PI * 2f; // randomize phase
}
void Update()
{
if (!target) return;
// Smooth follow with trailing
Vector3 desired = target.position + offset;
transform.position = Vector3.SmoothDamp(transform.position, desired, ref velocity, trailing, followSpeed);
// Add fake gravity/flap (only if oscillation is enabled)
if (!disableOscillation)
{
oscillationTime += Time.deltaTime * oscillationFrequency;
float yOsc = Mathf.Sin(oscillationTime) * oscillationAmplitude;
transform.position += new Vector3(0, yOsc, 0);
}
}
public void SetTargetTag(string tag)
{
targetTag = tag;
GameObject found = GameObject.FindGameObjectWithTag(targetTag);
if (found) target = found.transform;
}
}