How could I aim my hockey puck using the mouse?

I am currently working on a hockey-type game and am wondering how I could get the puck to shoot in the location that I click, and am having issues with bodyvelocity as it is new to me.

Server sided code:

script.Parent.Equipped:Connect(function()
	script.Parent.SE.OnServerEvent:Connect(function(plr,power,mhp)
		if game.Workspace.Puck:FindFirstChild("Attatched") then
			game.Workspace.Puck.Attatched:Destroy()
			game.Workspace.Puck.Canpick.Value = false
			wait(.4)
			game.Workspace.Puck.Canpick.Value = true
			game.Workspace.Puck.CanCollide = true
		end
		local force = Instance.new("BodyVelocity")
		force.MaxForce = Vector3.new(1,1,1) * math.huge
		force.Velocity = mhp
		force.Parent = game.Workspace.Puck
		force.P = power
		game.Debris:AddItem(force, .25)
	end)
end)

Local script code

local player = game.Players.LocalPlayer
local ms = player:GetMouse()
local db = false
local shooting = false
local power = 20
local hitpos = ms.Hit.Position
local whatshoulddo = ""
ms.Button1Down:Connect(function()
	power = 20
	if db == false then
		db = true
		shooting = true
	while shooting do
		wait(.15)
		power += 5
		if power == 100 or shooting == false then
				db = false
				if game.Workspace.Puck:FindFirstChild("Attatched") then
					
				if game.Workspace.Puck:FindFirstChild("Attatched").Part1 == player.Character.Stick.Blade then
				print(power)
				script.Parent.SE:FireServer(power, hitpos)
						power = 20
						shooting = false
					break
					else -- is for hitting, ignore
						if power >= 40 then
						print(power)
						whatshoulddo = "Hit"
							script.Parent.Send:FireServer(whatshoulddo, power)
							power = 20
							shooting = false
						end
					end
				else -- is for hitting, ignore.
					if power >= 40 then
						print(power)
							whatshoulddo = "Hit"
						script.Parent.Send:FireServer(whatshoulddo, power)
						power = 20
						shooting = false
					end

				end
			end
		end
	end
end)

ms.Button1Up:Connect(function()
	shooting = false
end)

(Some of the localscript code is for a different purpose which I labelled.)

1 Like

One way you can do it is have an attachment inside the puck and do

puck.CFrame = CFrame.lookAt(puck.Position, mouse.hit.p)
--Apply a forward force to the puck

So basically you are making puck turn to the position it needs to go, then applying a forward force, because now it’s forward is the position it needs to go to

Or if you want the puck to go an STOP exactly where you clicked, you can use CFrame.Lerp with mouse.hit.p

2 Likes

What else would I do on top of this like the velocity, and power. Really confused with bodyvelocity.

You can just do

puck.AssemblyLinearVelocity = puck.CFrame.LookVector*SPEED

assembly linear velocity is a property of a basepart, it applies pretty realistic force. Or you can use Moving constraints (linear velocity, vector force), read about them on dev hub

Sorry for the late reply, but how could I do this and make it go upwards at the same time as its going forwards?

this is supposed to be inside of ms.Button1Down:Connect(function() end) because everytime you fire server with hit position it sends the first one saved

instead of sending mouse.Hit.Position to the server send mouse.Hit.LookVector

local force = Instance.new("BodyVelocity")
force.MaxForce = Vector3.new(1,1,1) * math.huge
force.Velocity = mhp * power
force.Parent = game.Workspace.Puck
force.P = math.huge

Thanks a lot! This really helped a lot with making the shot work.