Bullet cannon script (Need help)

I am stuck in my game because I don’t know where to start as I don’t have that much experience with physics and math in games.

What I want to achieve is to make a bullet and spawn it and make it shoot up from a random location and go up like a parabolic fall physics where it curves down.

Note that, It should only move from X and Y axis so it may look like a 2D.

demo-1

  • white-lines are the calculated trajectory
  • green is the bullet
  • target from below is the random location (within range of min and max of X axis)
  • highest peak target icon is the calculated location where should the bullet must start to fall.
1 Like

Well, you could just simply use velocity for this.

1 Like

I did research this but the Velocity is Deprecated and I don’t know what to do next.

Use AssemblyLinearVelocity instead. I don’t know why the deprecated velocity to be honest.

Additionally, if you don’t want the players to be able to interfere with the cannons, you can place them in a separate collision group so that they cannot collide with it.

This is my first solution and it kinda works but it doesn’t seem to go upwards to the position of the part which defines the MaxHeight

local ePamato = ReplicatedStorage:WaitForChild("ePamato")

local Button = script.Parent
Button.MouseButton1Click:Connect(function()
	local bullet = ePamato:Clone()
	local lv = 50
	
	bullet.Position = workspace.SpawnArea.Position + Vector3.new(0, 0, math.random(-workspace.SpawnArea.Size.X/2, workspace.SpawnArea.Position.X/2))
	bullet.Parent = workspace
	
	local lvector = Vector3.new(0, lv, 0)
	
	task.spawn(function()
		game:GetService("RunService").RenderStepped:Connect(function()
			bullet.AssemblyLinearVelocity = bullet.AssemblyLinearVelocity:Lerp((bullet.Position - workspace.MaxHeight.Position).Unit * 50, 0.5)
		end)
	end)
	
	task.wait(2)
	bullet:Destroy()
end)
1 Like

Oh, if you want it to go to the max height, make the velocity the distance between the part and the target. This will make it go to the maxhieght.

1 Like

I’m really lost as to where should I put the specific value of velocity. any help?

1 Like

You would set the AssemblyLinearVelocity, as mentioned above, the AssemblyLinearVelocity is a property that allowed for the Velocity (used by the physics engine) to be set. This will cause the cannonball to be flung toward the target and fall down (due to gravity)

1 Like

Luckily I found this tutorial which is completely the one I’ve been looking for.

Projectile Physics - Roblox Scripting Tutorial

Glad you found a good tutorial on how to do this. Good luck with your game!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.