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

How come when I aim my part at the Direction of Mouse.UnitRay it doesnt aim there?

Asked by
Teeter11 281 Moderation Voter
9 years ago

What I am trying to do is aim the part at Mouse.UnitRay.Direction but it aims randomly into the ground and doesnt aim to my real unitray.Direction why is this?

Here is the code i used :


local oldDir = Mouse.UnitRay.Direction game:GetService("RunService").RenderStepped:connect(function() weld1.C1 = CFrame.new(Player.Character.Head.CFrame.p,oldDir):toObjectSpace(Player.Character.Torso.CFrame) oldDir = Mouse.UnitRay.Direction end)

2 answers

Log in to vote
2
Answered by
duckwit 1404 Moderation Voter
9 years ago

When you use the constructor CFrame.new(Vec3 from, Vec3 to), you need to be supplying the start position and the position to point towards, not the direction.

Additionally, it would be more time-efficient to store a reference to Player.Character.Head and Payer.Character.Torso, rather than refer to their full paths on every render step.

local oldDir = Mouse.UnitRay.Direction
local head = Player.Character.Head
local torso = Player.Character.Torso

game:GetService("RunService").RenderStepped:connect(function()
    local headPos = head.CFrame.p
    weld1.C1 = CFrame.new(headPos, headPos  + oldDir):toObjectSpace(torso.CFrame)

    oldDir = Mouse.UnitRay.Direction
end)
0
thanks :D Teeter11 281 — 9y
Ad
Log in to vote
-2
Answered by
neoG457 315 Moderation Voter
9 years ago
    local oldDir = Mouse.UnitRay.Direction

game:GetService("RunService").RenderStepped:connect(function()
        weld1.C1 = CFrame.new(Player.Character.Head.CFrame.p,oldDir):toObjectSpace(Player.Character.Torso.CFrame)


        oldDir = Mouse.UnitRay.Direction
    end)

I think your error may be on line 4 because you havent defined what "weld1" actually is so try this:

    local oldDir = Mouse.UnitRay.Direction

game:GetService("RunService").RenderStepped:connect(function()

local weld1 = Instance.new("Weld")
  weld1.C1 = CFrame.new(Player.Character.Head.CFrame.p,oldDir):toObjectSpace(Player.Character.Torso.CFrame)


        oldDir = Mouse.UnitRay.Direction
    end)

Sorry I may be wrong though.

0
There isnt an error that is just the weld that i use to aim the players head. Teeter11 281 — 9y

Answer this question