How do I make my cannon go back when shot?

So I have started making some cannons on my game and I have them fully scripted besides one thing, when it fires I want the cannon to go back a little bit and go forwards again, but since it’s a group I have no clue how to do that, can anyone tell me how?

1 Like

If you weld the parts to a central part you can apply the same methods you would usually use.

I attempted to use this but don’t know what kind of code to use to move a part? I haven’t used Vector3 or CFrame code before,

Cannon.CFrame *= CFrame.new(0,0,2)
1 Like

The easiest option would be to just use a reversible tween. Otherwise you just need a bit of maths, you could probably just apply some kind of parabolic function.

1 Like

As @PerilousPanther stated, a tween is probably best. You can change the EasingStyle to get the best movement.

Okay! I will try this out and let you know if it worked.

whats that?

A parabola is a mathematical graph, similar to what a rock thrown at a vertical angle will look like.
Google probably has a better description than I can give you.

1 Like

yea i think i need to be in university to understand that :confused:

Bounce would be the best option.

If you’ve ever seen a cannon recoil it doesn’t generally Bounce.
Quint or Exponential are probably the best bet, with EasingDirection Out.

@pie, it isn’t difficult to visualize, but yes, the calculations can be a bit daunting.

Not really. It’s pretty basic math:

local part = workspace.EasingPart

local X = part.Position.x

function Parabola(i)
	return (i)^2 + (X-25)
end

while wait(1) do
	for i=-5.2,5.2,0.2 do
		wait(0.01)
		part.Position = Vector3.new(Parabola(i), part.Position.Y, part.Position.Z)
	end
end

I would still suggest using tween though. It would be more efficient. You also remove a lot of the complexity involved in programming a parabolic function based on direction of fire.

1 Like