Basically I’m trying to make a system where you can swing off poles at high speeds to quickly change the direction you’re going, something like this:
However my main issue is making the player go around the pole in a circle. Originally I thought I could use :lerp() (I’m pretty new with it but I thought it could maybe work), but the problem is that the player gets closer and closer until they reach the pole, which I don’t want. What I mean:
Here’s the code:
--Code
if Click and closest ~= nil and not Swinging then --Click = if holding m1, closest = closest pole.
if (closest.Position - StreelightHitbox.LeftBox.Position).Magnitude < (closest.Position - StreelightHitbox.RightBox.Position).Magnitude then direction = "Left" end
Swinging = true
local relativePos = (hrp.Position - closest.Position)
if direction == "Right" then
hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(-relativePos.Z, 7, relativePos.X) * closest.CFrame, 1 * dT)
else
hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(relativePos.Z, 7, -relativePos.X) * closest.CFrame, 1 * dT)
end
elseif Click and Swinging then
local relativePos = (hrp.Position - closest.Position)
if direction == "Right" then
hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(-relativePos.Z, 7, relativePos.X) * closest.CFrame, 1 * dT)
else
hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(relativePos.Z, 7, -relativePos.X) * closest.CFrame, 1 * dT)
end
else
Swinging = false
end
--Code
Every frame, your Lerp is moving the character along a straight line path from where they currently are, to a point the same distance from the pole, but rotated 90 degrees around the pole. Picture the line connecting those two points, and what happens when you take a short step along that path. The path is not the circular arc you want, it’s the chord of that arc. So what happens is the character moves forward and a bit closer to the pole. Repeat this every frame, and what you get is the character spiraling inwards towards the pole, exactly like you see.
You want to rotate your character’s position around the pole as the pivot point, maintaining their distance from the pole, not translate them towards the goal.
The :Lerp() function interpolates the CFrame you give the function and the cframe you cast it on
A:Lerp(B,0)-- would be A
A:Lerp(B,1) -- would b B
A:Lerp(B,.5)-- would be halfway point
if you cast this every frame it’s just gonna eventually end with your character in the pole
what you probably should do is swing the character around the pole position with an offset, maybe physics constraints could work too but i’m not good with them so i don’t know
Your update would have a form something like: hrp.CFrame = CFrame.fromOrientation(0, dt * charSpeed / relativePos.Magnitude , 0) * ( hrp.CFrame - closest.Position) + closest.Position
The orientation CFrame here is a rotation around the world Y axis by an angle that is determined by the angular velocity of the swing rotation, which in this case I’ve just divided the character’s run speed (charSpeed here would be like their current linear velocity magnitude of the HRP, or some other value if you want the swing to have a specific tangential speed) by the radius of the swing, to get the equivalent angular velocity in Radians, which times the frame dt is how much they should rotate. This rotation is applied to the HRP, so it’s going to make the character turn as they go around the pole, but you could apply the rotation to only their position if you wanted them to stay facing the same world direction as they swing.
You also need to add a sign to the rotation, -1 or +1 for left and right, of course.