What do you want to achieve?
I want the blue part to move around the player with the part facing the mouse direction
What is the issue?
As you can see in this screenshot below the part is not facing the direction of the mouse and the back portion of the part is not lock to the HumanoidRootPart.
Basically the LookVector value of a CFrame will make the part face towards a coordinate position. You can read more about it here CFrame | Documentation - Roblox Creator Hub
local normalize = Vector3.new(1,0,1)
local function move()
local humanoidPosition = char.HumanoidRootPart.CFrame.p
local directionToMouse = Mouse.Hit.Position-humanoidPosition
local lookAtMouse = CFrame.lookAt(humanoidPosition,humanoidPosition+directionToMouse*normalize)
newTS.CFrame = lookAtMouse*CFrame.new(0,0,-newTS.Size.X/2)
end
Hmm, nvm let’s not add the size CFrame yet and just the look at CFrame which positions the newTS at the humanoid position and looking towards the mouse direction in a horizontal along the floor manner.
local normalize = Vector3.new(1,0,1)
local function move()
local humanoidPosition = char.HumanoidRootPart.CFrame.p
local directionToMouse = Mouse.Hit.Position-humanoidPosition
local lookAtMouse = CFrame.lookAt(humanoidPosition,humanoidPosition+directionToMouse*normalize)
local correctionCFrame = CFrame.new()--find one
newTS.CFrame = lookAtMouse*correctionCFrame
end
Also I’m not sure how your newTS part looks like and how you sized it.
Much better improvements but the part isn’t on the mouse.
local function move()
local humanoidPosition = char.HumanoidRootPart.CFrame.p
local directionToMouse = Mouse.Hit.Position-humanoidPosition
local lookAtMouse = CFrame.lookAt(humanoidPosition,humanoidPosition+directionToMouse*normalize)
local correctionCFrame = CFrame.new(newTS.Size.X/2,0,0)--find one
newTS.CFrame = lookAtMouse*correctionCFrame
end
So you resized it in the X direction instead of the size Z direction where the part is looking at the front? Then we will need another correction factor to rotate it anticlockwise around the parts Y axis.
local function move()
local humanoidPosition = char.HumanoidRootPart.CFrame.p
local directionToMouse = Mouse.Hit.Position-humanoidPosition
local lookAtMouse = CFrame.lookAt(humanoidPosition,humanoidPosition+directionToMouse*normalize)
local correctionCFrame = CFrame.new(newTS.Size.X/2,0,0)--find one
local rotationCorrectionCFrame = CFrame.Angles(0,math.rad(90),0)
newTS.CFrame = lookAtMouse*correctionCFrame*rotationCorrectionCFrame
end
Edit:
Hmm perhaps my CFrame order of operations is wrong try this:
No problem, just learn to mix and match CFrames and the techniques used here.
rotate it anticlockwise around the parts Y axis 90 degrees
local rotationCorrectionCFrame = CFrame.Angles(0,math.rad(90),0)
Removing the Y component from a direction vector
local normalize = Vector3.new(1,0,1)
--Vector humanoid to mouse position
local directionToMouse = Mouse.Hit.Position-humanoidPosition
local normalized = directionToMouse* normalize
--removes y axis so it only looks on XZ plane or along the floor
Mixing and matching CFrames mostly trial and error work with a bit of theory behind it, place model at humanoid first, then rotate it, then move it along the X axis according to it’s size.