Angleing a part to shoot projectile at player

(1/2) I am trying to make a mortar for my game. When I player is in range it will face towards the player and angle the barrel to fire. The projectiles will be given velocity and will be affected by gravity. There are two points of rotation on the mortar. I am able to make the mortar face towards the player but I do not know how to angle the barrel to shoot either up or down to hit the player. (Picture below of what I am trying to do)

(2/2) I trying to do something to like in this article to know how much to rotate the barrel so that the projectile to hit the player. Also, The projectiles always leave the barrel at the same speed.

Modeling a projectileā€™s motion ā† Article

Mortar - Roblox <ā€“M Model of what I have sofar

1 Like

You will have to use something like a CylindricalConstraint to achieve the motion that you want in both the vertical and horizontal axis; Then, once you calculate the desired ā€œendā€ goal, you can solve that in each plane to point towards them (So, you will rotate the base in the Y euler plane, and then rotate the barrel in the other plane).

Then, once you are happy, you should launch out a projectile at the desired velocity (with an Impulse force - perhaps via BasePart:ApplyImpulse ?) and use the force from the equation you can find in that fantastic EgoMoose article to figure out the required vector that will most likely hit your target.

1 Like

How would I go about calculating that though? (Thatā€™s my main problem Iā€™m only in alg 2 :sob: )

The equation is given in the article you linked; Iā€™ve simplified the code a bit:

local Origin = Vector3.new(0,0,0)
local TargetPosition = Vector3.new(4,0,4)

local Gravity = Vector3.new(0, -game.Workspace.Gravity, 0)

local Velocity = (TargetPosition - Origin - 0.5 * Gravity) 
-- This is your velocity vector.
1 Like

No, but the velocity will always be what way the barrel is facing and the speed coming out of the barrel will stay constant. What I am trying to do is get an equation for the rotation of the barrel based on how far away the player is.

Ah I see - this is a little more complex. Iā€™d recommend using a CFrame to achieve this:

local CF = CFrame.new(PositionOfBase, PositionOfBase + Velocity)

That will give you the CFrame Angle to point towards it - you can then either use that CFrame directly (without constraints) or turn it into angles.

1 Like

Ah yes, just to be clear this is the type of frame with start, target?

ā€“ o is just the wrecking ball that the motor shoots
ā€“ workspace.Part is the part that the wrecking ball goes to with AlignPosition

workspace.o.Touched:Connect(function(object)
if object == ā€œBaseplateā€ then
workspace.o.CanTouch = false
workspace.o.VectorForce:Destroy()
workspace.oCanTouch = true
end
end)

while wait() do
if (workspace.o.Attachment.WorldPosition - workspace.Part.Attachment.WorldPosition).Magnitude < 3 then
workspace.o.AlignPosition:Destroy()
break
end
end

Basically, what you should do is everytime the motor shoots clone it and parent it to the workspace, then add a VectorForce and AlignPosition. Make a part that will be half the distance from the player and wrecking ball, and make the Y axis high so the wrecking ball goes high. Add the first attachment to the wrecking ball part and the second attachment to the part thatā€™s half the distance. After the ballā€™s attachment is close enough to the partā€™s attachment, destroy the AlignPosition. When the ball hits the ground, destroy the VectorForce.

I got a brilliant idea, what if you modeled the equation of the intersection by using quadratics (the vertex will be the players position). Then, calculate the derivative and use the plane slope equation to get 2 points along that slope. Then construct a vector from those points and align the object along this vector

This is great, but not the effect Iā€™m trying to achieve.

Mortar - Roblox I did everything you said, but it is lagging and does not work well.