How do I make the part move around the player while still having the part facing the mouse direction?

  1. What do you want to achieve?
    I want the blue part to move around the player with the part facing the mouse direction

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

    https://gyazo.com/1d8519a8e49c5349e23cca42c9ffe288

The script I’m currently using

local function move()
	local vector = Vector3.new(Mouse.Hit.X,char.HumanoidRootPart.Position.Y -1, Mouse.Hit.Z) - char.HumanoidRootPart.CFrame.p
	newTS.CFrame = CFrame.new(char.HumanoidRootPart.Position + vector.Unit * newTS.Size.X/2)
	newTS.Orientation = vector.Unit
end

try using lookvector on the part from the root part to the mouse

Yea… I’m not sure how to do that TBH :sweat_smile:

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

Please use variables.

   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

https://gyazo.com/96427b45348c4f39da44c0a6bef3dc98

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.


If you resized it in the front I’m guessing the correction CFrame to move it in front of the player will be:

local correctionCFrame = CFrame.new(0,0,-newTS.Size.Z/2)

In order to move the part half the blocks size in the front direction forwards.

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

https://gyazo.com/52bd0f45264c9381fcf2f71ed521274f

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:

						newTS.CFrame = lookAtMouse*rotationCorrectionCFrame*correctionCFrame

1 Like
local function move()
	local humanoidPosition = char.HumanoidRootPart.CFrame.p
	newTS.CFrame = Mouse.Hit
	local Nc = CFrame.new(Vector3.new(humanoidPosition.X, newTS.Position.Y, humanoidPosition.Z), newTS.Position)
	Nc = Nc - Nc.Position
	newTS.CFrame = Nc + newTS.Position + Vector3.new(0,newTS.Size.Y/2,0)
end

Place.rbxl (22.2 KB)

Works perfectly! Thank you!!!

No problem, just learn to mix and match CFrames and the techniques used here.

  1. rotate it anticlockwise around the parts Y axis 90 degrees
local rotationCorrectionCFrame = CFrame.Angles(0,math.rad(90),0)
  1. 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
  1. 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.
newTS.CFrame = lookAtMouse*rotationCorrectionCFrame*correctionCFrame

Once you know these you are on track to mastering CFrames. There’s a lot more but that’s for another day or another scripting support help :stuck_out_tongue:

2 Likes