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

How can I use Raycasting to point a model towards the mouse position? (Unsolved)

I've been working with making a custom combat system, and have had some issues with getting the weapons to point correctly.

I've tried making the physical tool a model (other than handle) and setting a primary part, then setting its CFrame to the calculated distance from the character, yet to no avail.

I'm not asking this to be made for me of course, I'd just love to know how I could generally accomplish coding this.

Thank you for reading, and I hope to find a solution!

EDIT: Current code:

mouse.Move:connect(function()
	if mouse.Hit then
	script.Parent.spear:SetPrimaryPartCFrame(CFrame.new(script.Parent.spear:GetPrimaryPartCFrame().p, mouse.Hit.p))
	end
end)

(mouse is defined earlier in the script)

1 answer

To do this, I recommend setting the CFrame of the PrimaryPart. There are two important methods of Model objects while doing this: SetPrimaryPartCFrame and GetPrimaryPartCFrame.

When you set the PrimaryPart's CFrame via SetPrimaryPartCFrame, every other part in the model gets it's CFrame changed relative to the PrimaryPart. To make it look at a point you can use the constructor CFrame.new(Vector3 position, Vector3 point).

local model = game.Workspace.Model
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:connect(function()
	if mouse.Hit then
		model:SetPrimaryPartCFrame(CFrame.new(model:GetPrimaryPartCFrame().p, mouse.Hit.p))
	end
end)

5 comments

0

I have tried line 6 in your provided code, however, to no avail. After looking anything up that I was slightly unsure of, I don't see why it wouldn't function. I feel that it might be in part to the weapon being welded by script/CFrame (OzzyPig's Handle Welding Plugin), but it's hard to say for sure.

0

Oh! It's a tool. I thought it was just a model in workspace.

0

Oh, heh, I probably should have directly stated that. Thank you for the answer, regardless!

0

I can try to fix my answer. Were you looking for the arm to point where the mouse is, or just the tool itself?

0

It's not really that important, I'd prefer both, but can assume that the code required would be very similar between the two.

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