How to fix my quadratic motion formula?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to make a cannon that follows an arc using the quadratic equation

  1. What is the issue? Include screenshots / videos if possible!

The current height prints out 240ish at t-0 instead of 0. I am wondering if I missed to convert the height unit in studs to feet but cant seem to figure out how to solve it.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local ProjectileService = {}

local RunService = game:GetService("RunService")

function ProjectileService:FireProjectile(startingPos, Force)
	local projectile = script.Projectile:Clone()
	projectile.Position = startingPos
	projectile.Parent = workspace
	
	local connection
	
	local t = tick()
	local startingV = 150 * 1.4
	local startingH = startingPos.Y
	
	connection = RunService.Stepped:Connect(function()
		local timeinSeconds = tick() - t
		
		--currentHeight = quadratic coefficent * gravity(m/s^2) * TimeInSeconde^2 + (StartingVelocity m/s^2 * TimeInSeconds) + height
		
		local currentHeight = (-.5) * (54.936) * (timeinSeconds^2) + (startingV * timeinSeconds) + 0 
		print(currentHeight)
		
		--projectile.CFrame = CFrame.new(projectile.Position.X, currentHeight, projectile.Position.Z)
		
		if currentHeight < 1 then
			connection:Disconnect()
		end
	end)
end

return ProjectileService

Try debug graphing the equation, this resource should help in the future.

I went to reply to your now-deleted thread earlier, but assuming this one is sticking around…

Methinks this is a problem with your timing mechanism. The math seems kosher. Instead of using tick(), you should use workspace.DistributedGameTime.

local startTime = workspace.DistributedGameTime
-- ...
connection = RunService.Stepped:Connect(function()
    local deltaTime = workspace.DistributedGameTime = startTime
    -- ...
end)
...

It may be worth your effort to look at Debugging Tools, namely the Lua Debugger which will help you look for clues.

Can you post how you call this function?
I just tested this myself and for me it actually does start at 0

As you can see here:
image

I have made no change to the code other than adding my own print and commenting out “connection:Disconnect()”

I simply call the function fireprojectile in a server script in serversciptservice like so

local ProjectileService = require(script.Parent.ProjectileService)

ProjectileService:FireProjectile(workspace.Part.Position, 500)

it turns out @Ozzypig that using distributed game time was a better option. One more question would be how I can remove the choppyness of the part moving across the arc. I thought it would be smoooth as stepped fired every 1/60th of a second

1 Like

Sounds like a job for tweening to smoothen the CFrames

1 Like