Rope breaking works now

This commit is contained in:
Michal Pikulski
2025-09-21 23:37:02 +02:00
parent ef3b50b99f
commit 38f578c56a
2 changed files with 75 additions and 12 deletions

View File

@@ -107,7 +107,11 @@ public class RopeBreaker : MonoBehaviour
/// </summary>
private void CreateBreakPointTransform(Vector3 breakPointPosition)
{
// Create a new GameObject for the break point
// Store references to the original rope endpoints
Transform originalStartPoint = originalRope.StartPoint;
Transform originalEndPoint = originalRope.EndPoint;
// Create a new GameObject for the break point (attached to Player)
GameObject breakPointObj = new GameObject("RopeBreakPoint");
breakPointTransform = breakPointObj.transform;
breakPointTransform.position = breakPointPosition;
@@ -115,12 +119,15 @@ public class RopeBreaker : MonoBehaviour
// Add the physics follower component to the break point
RopeEndPhysicsFollower follower = breakPointObj.AddComponent<RopeEndPhysicsFollower>();
follower.targetTag = "Player";
// Set specific transform to follow instead of using tag
follower.SetTargetTransform(originalStartPoint);
follower.followSpeed = ropeFollowSpeed;
follower.trailing = ropeTrailing;
follower.useGravity = false; // Player rope end doesn't use gravity
follower.useGravity = true; // Player rope end doesn't use gravity
// Create second break point
// Create second break point (for the rock-attached end)
GameObject secondBreakObj = new GameObject("RopeBreakPoint_Second");
secondBreakTransform = secondBreakObj.transform;
secondBreakTransform.position = breakPointPosition;
@@ -128,7 +135,10 @@ public class RopeBreaker : MonoBehaviour
// Add physics behavior to second break point
RopeEndPhysicsFollower secondFollower = secondBreakObj.AddComponent<RopeEndPhysicsFollower>();
secondFollower.targetTag = "Rock";
// Set specific transform to follow instead of using tag
secondFollower.SetTargetTransform(originalEndPoint);
secondFollower.followSpeed = ropeFollowSpeed;
secondFollower.trailing = ropeTrailing;
secondFollower.useGravity = true; // Enable gravity for hanging rope end
@@ -139,7 +149,7 @@ public class RopeBreaker : MonoBehaviour
secondFollower.forceYReset = forceYReset;
// Create initial separation
Vector3 direction = (originalRope.EndPoint.position - breakPointPosition).normalized;
Vector3 direction = (originalEndPoint.position - breakPointPosition).normalized;
if (direction.magnitude < 0.01f) direction = Vector3.down;
breakPointTransform.position -= direction * initialSeparationDistance * 0.5f;

View File

@@ -4,8 +4,10 @@ using UnityEngine;
public class RopeEndPhysicsFollower : MonoBehaviour
{
[Header("Target Settings")]
[Tooltip("Tag of the object this endpoint should follow")]
[Tooltip("Tag of the object this endpoint should follow (used if targetTransform is not set)")]
public string targetTag;
[Tooltip("Transform this endpoint should follow (takes precedence over targetTag)")]
public Transform targetTransform;
[Tooltip("How quickly the endpoint follows the target")]
public float followSpeed = 5f;
[Tooltip("How much trailing (0 = instant, 1 = very slow)")]
@@ -81,10 +83,20 @@ public class RopeEndPhysicsFollower : MonoBehaviour
if (debugLog) Debug.Log("[RopeEndPhysicsFollower] No attached rope found, using default max distance");
}
if (!string.IsNullOrEmpty(targetTag))
// Use targetTransform if set, otherwise try to find by tag
if (targetTransform != null)
{
target = targetTransform;
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] Using assigned target transform: {target.name}");
}
else if (!string.IsNullOrEmpty(targetTag))
{
GameObject found = GameObject.FindGameObjectWithTag(targetTag);
if (found) target = found.transform;
if (found)
{
target = found.transform;
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] Found target by tag '{targetTag}': {target.name}");
}
}
// Initialize offset and velocities
@@ -109,14 +121,14 @@ public class RopeEndPhysicsFollower : MonoBehaviour
transform.position = pos;
}
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] Initialized with tag {targetTag}, gravity enabled, initial Y velocity: {physicsVelocity.y}");
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] Initialized with target: {target.name}, gravity enabled, initial Y velocity: {physicsVelocity.y}");
}
}
else
{
offset = Vector2.zero;
lastTargetPosition = transform.position;
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] No target found with tag {targetTag}");
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] No target found");
}
velocity = Vector3.zero;
@@ -159,11 +171,18 @@ public class RopeEndPhysicsFollower : MonoBehaviour
// 1. Gravity - always pulls down
physicsVelocity.y -= gravityStrength * deltaTime;
// 2. Vertical hanging force - try to align X with target when stationary
// 2. Vertical hanging force - try to align X with target (not with world origin)
if (Mathf.Abs(targetVelocity.x) < 0.1f)
{
// Use the actual X position of the target as the desired X position
float xOffset = transform.position.x - target.position.x;
physicsVelocity.x -= xOffset * verticalHangStrength * deltaTime;
// Debug log to track vertical hanging behavior
if (debugLog && Time.frameCount % 120 == 0)
{
Debug.Log($"[RopeEndPhysicsFollower] Vertical hanging: target X={target.position.x}, my X={transform.position.x}, offset={xOffset}");
}
}
// 3. Rope length constraint - apply a force toward the target if we're exceeding the rope length
@@ -231,12 +250,46 @@ public class RopeEndPhysicsFollower : MonoBehaviour
}
}
public void SetTargetTransform(Transform newTarget)
{
targetTransform = newTarget;
target = newTarget;
if (target != null)
{
lastTargetPosition = target.position;
// Only update horizontal offset to maintain current vertical position
if (initialized)
{
Vector2 newOffset = transform.position - target.position;
offset.x = newOffset.x;
// Don't update offset.y to allow gravity to work
}
else
{
Vector2 newOffset = transform.position - target.position;
offset.x = newOffset.x;
offset.y = 0; // Don't preserve vertical offset for gravity simulation
}
// Apply initial falling impulse if using gravity
if (useGravity)
{
physicsVelocity = new Vector2(physicsVelocity.x, -initialFallImpulse);
if (debugLog) Debug.Log($"[RopeEndPhysicsFollower] Reset Y velocity to {physicsVelocity.y} after target change to {target.name}");
}
}
}
// Original tag-based method kept for backward compatibility
public void SetTargetTag(string tag)
{
targetTag = tag;
GameObject found = GameObject.FindGameObjectWithTag(targetTag);
if (found)
{
targetTransform = found.transform;
target = found.transform;
lastTargetPosition = target.position;