[Diving] Movement with a rock and 3 ropes is working

This commit is contained in:
Michal Pikulski
2025-09-04 23:13:19 +02:00
parent d34eb77e20
commit fbb658098b
110 changed files with 28707 additions and 4 deletions

View File

@@ -4,6 +4,12 @@ public class EndlessDescenderController : MonoBehaviour, ITouchInputConsumer
{
private float targetFingerX;
private bool isTouchActive;
private float originY;
void Awake()
{
originY = transform.position.y;
}
void Start()
{
@@ -44,8 +50,14 @@ public class EndlessDescenderController : MonoBehaviour, ITouchInputConsumer
moveStep = Mathf.Clamp(moveStep, -absOffset, absOffset);
float newX = currentX + moveStep;
newX = Mathf.Clamp(newX, GameManager.Instance.EndlessDescenderClampXMin, GameManager.Instance.EndlessDescenderClampXMax);
Vector3 oldPos = transform.position;
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
float newY = originY;
// Add vertical offset from WobbleBehavior if present
WobbleBehavior wobble = GetComponent<WobbleBehavior>();
if (wobble != null)
{
newY += wobble.VerticalOffset;
}
transform.position = new Vector3(newX, newY, transform.position.z);
// Debug.Log($"EndlessDescenderController: Moved from {oldPos} to {transform.position} (targetX={targetX}, lerpSpeed={lerpSpeed}, offset={offset}, exponent={exponent}, moveStep={moveStep})");
}

View 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);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f1a7937fe5974e9486dc7545de8cee6b
timeCreated: 1757020153

View File

@@ -0,0 +1,68 @@
using UnityEngine;
public class WobbleBehavior : MonoBehaviour
{
[Header("Wobble Settings")]
public float wobbleFrequency = 1.5f; // How fast the bottle rocks
public float baseWobbleAmplitude = 8f; // Max degrees from horizontal
public float speedToAmplitude = 2f; // How much speed affects amplitude
public float maxRotationLimit = 45f; // Maximum allowed rotation in degrees
[Header("Vertical Movement Settings")]
public float verticalFrequency = 0.5f; // How fast the bottle moves up/down
public float verticalAmplitude = 0.5f; // How far the bottle moves up/down
[Header("Smoothing Settings")]
public float velocitySmoothing = 10f; // How quickly velocity is smoothed
public float rotationSmoothing = 10f; // How quickly rotation is smoothed
private Vector3 lastPosition;
private float wobbleTime;
private float velocity;
private Vector3 basePosition;
private float verticalOffset;
private float smoothedVelocity;
private float smoothedAngle;
public float Velocity => velocity;
public float VerticalOffset => verticalOffset;
void Start()
{
lastPosition = transform.position;
smoothedVelocity = 0f;
smoothedAngle = 0f;
}
void Update()
{
// Calculate movement speed (exclude vertical wobble from velocity calculation)
Vector3 horizontalPosition = transform.position;
horizontalPosition.y = 0f; // Ignore Y for velocity if only horizontal movement matters
Vector3 horizontalLastPosition = lastPosition;
horizontalLastPosition.y = 0f;
velocity = (horizontalPosition - horizontalLastPosition).magnitude / Time.deltaTime;
lastPosition = transform.position;
// Smooth velocity to prevent jitter
smoothedVelocity = Mathf.Lerp(smoothedVelocity, velocity, velocitySmoothing * Time.deltaTime);
// Wobble amplitude scales with smoothed speed, but always has a base value
float amplitude = baseWobbleAmplitude + smoothedVelocity * speedToAmplitude;
amplitude = Mathf.Min(amplitude, maxRotationLimit); // Prevent amplitude from exceeding limit
// Oscillate around horizontal (0 degrees)
wobbleTime += Time.deltaTime * wobbleFrequency;
float targetAngle = Mathf.Sin(wobbleTime) * amplitude;
targetAngle = Mathf.Clamp(targetAngle, -maxRotationLimit, maxRotationLimit);
// Smooth the rotation angle
smoothedAngle = Mathf.Lerp(smoothedAngle, targetAngle, rotationSmoothing * Time.deltaTime);
// Apply rotation (Z axis for 2D)
transform.localRotation = Quaternion.Euler(0f, 0f, smoothedAngle);
// Calculate vertical up/down movement (wave riding) only once
verticalOffset = Mathf.Sin(wobbleTime * verticalFrequency) * verticalAmplitude;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9072050a53fc4b539f4f4716bab53c07
timeCreated: 1757017852