I’m working on a code where if my player attacks and there is a hook object, the player will then transition into state where they’re swinging on the hook from a rope. I’m using a distance joint and a line renderer to draw the rope. Everything works perfectly except that once the line is drawn, the position of the line does not update when the player moves. The line.SetPosition gets the point of the hook when it initially connects, but won’t update after that. I need it to update with the player’s transform so it looks like the player is swinging on the rope. hookPos is the player, hook is the hook object that the overlap circle detects. Any ideas on how I can get this to update each frame?
Here’s the code:
void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetButtonDown("Attack"))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
Collider2D[] hookGrapple = Physics2D.OverlapCircleAll(HorizontalAttackPoint.position, hookSize, hookLayers);
if (isFacingUp == false && isFacingDown == false)
{
hookGrapple = Physics2D.OverlapCircleAll(HorizontalAttackPoint.position, hookSize, hookLayers);
}
foreach(Collider2D hook in hookGrapple)
{
joint.enabled = true;
line.enabled = true;
joint.connectedAnchor = hook.transform.position;
line.positionCount = 2;
line.SetPosition(0, hookPos.transform.position);
line.SetPosition(1, hook.transform.position);
}
}
}