Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
RoboFrog 400 Moderation Voter
9 years ago

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

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

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)

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. RoboFrog 400 — 9y
0
Oh! It's a tool. I thought it was just a model in workspace. BlackJPI 2658 — 9y
0
Oh, heh, I probably should have directly stated that. Thank you for the answer, regardless! RoboFrog 400 — 9y
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? BlackJPI 2658 — 9y
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. RoboFrog 400 — 9y
Ad

Answer this question