Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to send projectiles to mouse position?

Ok so I'm making a gun for my game, and I already have a script that shoots a projectile out of the barrel, but I don't just want it to go forward, since it's a 3rd person game and it'd be impossible to aim, but instead the projectile would travel to to the player's mouse in the workspace. Is there any way to do this?

1 answer

There are many type of proyectiles, such as Raycast bullets, parts moved with BodyVelocity...

In case you want a rocket-like proyectile, you may need a script like this:

local player = game:GetService("Players").LocalPlayer
local tool = script.Parent

local speed = 30

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		local rocket = Instance.new("Part",workspace)
		rocket.Name = "Rocket"
		rocket.Size = Vector3.new(1,1,3)
		
		local mousePos = mouse.Hit.p

		--Setting the angle between the mouse and the Handle

		local angX,angY,angZ = CFrame.new(tool.Handle.Position,mousePos):ToEulerAnglesXYZ()
		
		rocket.CFrame = CFrame.new(tool.Handle.Position) * CFrame.Angles(angX,angY,angZ)
		
		local force = Instance.new("BodyVelocity",rocket)
		force.MaxForce = Vector3.new(400000,400000,400000)

		force.Velocity = (mouse.Hit.p - tool.Handle.Position).unit * speed
		
		rocket.Touched:Connect(boom)
	end)
end)

function boom(hit)
	print("Proyectile collided with "..hit.Name.."!")
end

Otherwise, If you need a proyectile that reaches It's target in almost no time, then you need Raycasting:

https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox

1 comment

0

I said my script was already working, I just needed a way to send the projectile to the mouse.

This discussion is preserved as it was. New answers, comments, and votes are closed.