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

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

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

✓ Accepted answer

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)

1 comment

0

thanks :D

    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.

1 comment

0

There isnt an error that is just the weld that i use to aim the players head.

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