Moving Part To A Target In An Arc

Hello! I’m trying to make a basketball-like game but I’m stuck on one part. How would I make one part (the basketball) shoot up and eventually land in the hoop (second part)? I want it to be like the arc you get when you shoot a basketball in real life.


Excuse the great drawing. Ik it’s easy to make it follow the red path into the target, but I need it to follow the blue path from any point on the map. How could i do this?

Thank you for helping :sunglasses:

1 Like

We can do it with a little bit of math: math.sin(math.pi * t)!

For X and Z, we can lerp normally but we will use the math above to modify Y to make the arc.

local accumDt = 0
local lerpTime = 5

function lerp(a, b, t)
	return a + (b - a) * t
end

local startPart = workspace.Start
local finishPart = workspace.Finish

local movePart = workspace.Move

local yOffset = 5 -- add height to ball, for making arc

local renderConnect:RBXScriptConnection

renderConnect = game:GetService("RunService").Heartbeat:Connect(function(dt)
	accumDt = math.min(accumDt + (dt / lerpTime), 1) -- maximum 1
	
	local lerpedPos = startPart.Position:Lerp(finishPart.Position, accumDt)
	
	local lerpedPosY = lerpedPos.Y -- modify Y position to not make it go straight like X and Z axis
	local slerpYOffset = math.sin(math.pi * accumDt) * yOffset -- arc math here
	
	movePart.Position = Vector3.new(lerpedPos.X, lerpedPosY + slerpYOffset, lerpedPos.Z) -- assemble again!
	
	if accumDt == 1 then
		renderConnect:Disconnect()
		print("finished!")
	end
end)

You can check out the demo below:
Arc Fly.rbxl (53.4 KB)

1 Like

Thanks I’m going to try it. Thanks for helping

No problem. Ask if you don’t understand.

Oh yeah btw is there a way I could make it collide with things since right now if there is something like an obstacle in the way it phases through it thanks

Then, you are going to need to make your own physics by using raycasts

1 Like

Not gonna lie, thats just moving the part to a desired location without any physics meaning it cant miss or anything so nothing like a basketball. If you are looking for phyics thats different

For your slerpYOffset, the equation is basically y = 5sin(πx) with an amplitude of 5 and a period of π radians right

Instead of setting the movePart.Position to the next location, you can make a Tween that does it over the dt timeframe.

If it’s a basketball, you can add this basketball:Touched function

movePart.Touched:Connect(function(otherPart)
if otherPart == Backboard then
           stopArc = true -- check this boolean when calculating the position of ball, 
--so that it stop its arc when landing in the goal, in the function where 
--you are calculating the ball arc

 -- Normal gravity physics will take over since stopArc = true, 

--the basketball can hit the backboard and still later fall through the goal
-- You may want to apply the velocity that the ball should have here 
--so that the ball doesn't just unnaturally start falling down with an 
--initial velocity of 0 after obviously travelling through the air
end 
else if otherPart == Goal then    -- Have a part in the goal that will detect if a 
-- basketball lands in the hoop
        stopArc = true  
        goalLanded = true   -- check this boolean when calculating the position
 --of ball, so it sets up the flag for checking to add score
end
-- Here you can do another Tween that will animate the ball from a place 
--defined inside the hoop to below on the ground

if goalLanded then
          Score()    -- some score function to add point to whatever team scored
)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.