Hey all, I’m working on a game where a character can swing around a pole, and it’s important that it be physics based. Here’s what I’ve worked out so far:
void FixedUpdate()
{
//force due to gravity
Vector3 gForce = gravity * Vector3.down;
//vector from grip point to current position
Vector3 offset = transform.position - gripPoint.position;
//get a normalized vector perpendicular to the offset (in the transform's forward direction)
Vector3 moveDirection = Vector3.Cross(offset, transform.right).normalized;
//get the component vector of gravity in the move direction
Vector3 hForce = Vector3.Dot(gForce, moveDirection) * moveDirection;
//calculate the velocity in the forward direction and use it to calculate the centripetal force on the object
float hVelocity = Vector3.Dot(velocity, moveDirection);
Vector3 vForce = -hVelocity * hVelocity * offset.normalized / offset.magnitude;
//add the forward force and the centripetal force
Vector3 velocityDelta = (hForce + vForce) * Time.deltaTime;
//add the acceleration to the current velocity
velocity += velocityDelta;
//change position based on velocity
transform.position += velocity * Time.deltaTime;
}
It works for the most part, but the pendulum loses momentum, and slowly drops over time. I can manually keep it at the same distance, but it still loses momentum. Can anyone help me figure out if I’m doing something wrong? Or suggestions for a better way to do it?