Hey guys, I need some help with the maths for my projectile system. All I want it to do for now is to launch a projectile towards the mouse - it works fine horizontally, but there’s a weird vertical offset the higher you shoot and I’m not sure what’s causing it or how to fix it.
Here’s a video of the problem:
And here’s the code:
local VELOCITY = 110
function Utils:ThrowProjectile(destination: Vector3, origin: Vector3)
-- TEMPORARY
local projectile = Instance.new("Part")
projectile.Massless = true
projectile.Anchored = true
projectile.CanCollide = false
projectile.CanQuery = false
projectile.Color = Color3.fromHSV(0, 1, 0.5)
projectile.Material = Enum.Material.Neon
projectile.Size = Vector3.new(0.3, 0.3, 0.3)
projectile.Shape = Enum.PartType.Ball
local velocityUnits = (destination - origin).Unit
local angle = math.rad(90 * velocityUnits.Y)
local verticalVelocity = VELOCITY * math.sin(angle)
local horizontalVelocity = VELOCITY * math.cos(math.abs(angle))
projectile.Position = origin
projectile.Parent = workspace
while projectile.Position.Y > 0 do
local delta = task.wait()
local movementVector = Vector3.new(velocityUnits.X * horizontalVelocity * delta, verticalVelocity * delta, velocityUnits.Z * horizontalVelocity * delta)
projectile.Position = projectile.Position + movementVector
end
end
I have already checked and the function for returning the mouse’s position is correct, so it’s a problem with the maths and not with the destination/origin input. I’d appreciate any help!