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)
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)
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.