Math & Physics
Intermediate
Unity 2021.3+ / Unity 6
와이어 로프 진자 운동 시뮬레이션 및 릴리즈 횡속도 증폭 관성 물리
2D 힌지/로프 조인트 기반 진자 궤적에서 접선 벡터(Tangent Vector)를 추출하고 로프 해제 시 횡가속도(Horizontal Boost)를 보존하는 관성 공식.
#Math #Physics #2D Platformer #Mechanics #Vector
핵심 개념로프 액션 게임의 손맛은 '로프를 놓았을 때 관성이 폭발적으로 튀어나가는가'에서 결정됩니다. 플레이어 위치와 앵커 좌표 사이의 방향 벡터에 수직인 접선 벡터(Tangent) 방향으로 스윙 힘을 가하고, 릴리즈 시 횡방향 속도에 증폭 계수(
swingJumpBoost)를 곱해 탄력을 부여합니다.1. 관성 스윙 물리 계산 C# 구현
using UnityEngine;
public class RopeSwingPhysics : MonoBehaviour
{
[SerializeField] private Rigidbody2D rb;
[SerializeField] private float swingForce = 25f;
[SerializeField] private float swingJumpBoost = 1.35f;
[SerializeField] private float maxSwingSpeed = 22f;
public void ApplySwingForce(Vector2 anchorPos, float horizontalInput)
{
Vector2 ropeDir = (anchorPos - (Vector2)transform.position).normalized;
// 로프 벡터에 수직인 접선 벡터(Tangent: 90도 회전)
Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x);
// 입력 방향과 접선 방향 동기화
if (tangent.x * horizontalInput < 0f)
{
tangent = -tangent;
}
rb.AddForce(tangent * (swingForce * Mathf.Abs(horizontalInput)), ForceMode2D.Force);
// 속도 상한 캡핑
if (rb.linearVelocity.magnitude > maxSwingSpeed)
{
rb.linearVelocity = rb.linearVelocity.normalized * maxSwingSpeed;
}
}
public void ReleaseRope()
{
Vector2 v = rb.linearVelocity;
// 횡방향 가속도 증폭 보존
v.x *= swingJumpBoost;
if (v.y > 0f) v.y *= 1.1f;
rb.linearVelocity = v;
}
}