Firing the player out a Cannon

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

  1. **What do you want to achieve? I have a system that uses apply impulse to fire the players character, the problem is that it is inaccurate and the farther I need to shoot the player the worse the accuracy gets.

  2. What is the issue? Include screenshots / videos if possible!
    This is the way I am firing the player, I want them to end up exactly at the end point.


cannon.FireCannon = function(plr, startPos, EndPos)
	
	local direction = (EndPos - startPos)
	local horizontalDistance = (Vector3.new(direction.X, 0, direction.Z)).Magnitude
	local heightDifference = (Vector3.new(0, direction.Y, 0)).Magnitude
	local gravity = game.Workspace.Gravity

	local duration = math.log(1.001 + horizontalDistance * 0.01)

	local normalizedDirection = direction.Unit

	local horizontalForce = horizontalDistance / duration
	local verticalForce = (gravity * duration * 0.5) + (heightDifference / duration) * 2

	local force = Vector3.new(
		horizontalForce * normalizedDirection.X,
		verticalForce,
		horizontalForce * normalizedDirection.Z
	)
	
	local char = plr.Character
	local ogSpeed = char.Humanoid.WalkSpeed
	local mass = 0

	
	char.Humanoid.WalkSpeed = 0
	task.wait(.6)
	
	task.spawn(camFollow, char, EndPos, duration) --ignore this
	
	task.wait(.11)
	print(char.HumanoidRootPart.Mass)
	for i, v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") then
			mass += v.Mass
		end
	end
	print(mass)
	--	print(char.AssemblyMass)
	char.HumanoidRootPart:ApplyImpulse((force * mass * 1.009))

	
	task.wait(duration)
	char.Humanoid.WalkSpeed = ogSpeed
	char.HumanoidRootPart.Position = EndPos
end
  1. What solutions have you tried so far? I have looked through the forums but cant quite find anything useful.
1 Like

You could try to tween the arc to your liking, so it always follow a set path.

I figured this would be my only option, how would I simulate gravity using tween?

immo it’d be best to run it on every frame with a renderstepped rather than a tween,
then you could use a sine wave to get the player’s Y offset and maybe apply some easing to it later

So;

  1. use linear interpolation to move the player towards the target
  2. offset the player’s Y Coordinate using a sin function
  3. apply easing

Here is what i managed to do:
canon.rbxm (4.6 KB)

Altough it’s not the smoothest and doesn’t feel realistic
Play around with the easing (btw you should ease the p value)

EDIT: just tried Heartbeat instead of renderStepped and its way smoother, so make sure to change that

1 Like

Thanks man, what I made would work like 10% of the time so this is really helpful

1 Like