(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.
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.
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.
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.
ā 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