How to face part to root's face position

Hey there! so, im not too familiar with CFrames so i came to the dev forum to ask these questions. i have no idea on how to do this.

how would i face the part to the root face’s front? I have searched and tried everything and it still doesn’t work.

Thanks!!

1 Like

There’s a CFrame method called lookAt that does exactly this. It takes two Vector3s as arguments:

local origin = workspace.SomePart.Position
local lookAtPoint = (targetChar.PrimaryPart.CFrame * CFrame.new(0, 0, -1)).Position

workspace.SomePart.CFrame = CFrame.lookAt(origin, lookAtPoint)

If I didn’t multiply it by the right axis, you can modify the offset until the results look appropriate.

2 Likes

Doesn’t work, i don’t see the part idk why

(Client)

while task.wait() do
		if cg and root then
			local origin = cg.Position
			local lookAtPoint = (root.CFrame * CFrame.new(0, 0, -1)).Position

			cg.CFrame = CFrame.lookAt(origin, lookAtPoint)
		end
	end
1 Like

It works fine for me. Any output??

No, wonder if its because of the cg properties
image

Where’s your script located?

fillchars

1 Like

StarterCharacterScripts

also, i just realized it only rotates the part, how would i make it so it goes into the player too?

i tried

cg.CFrame = root.CFrame

didn’t work

Do you want the part to gradually move towards the player’s facing direction??

1 Like

Yes

sorry for not actually making it clear in the post :pensive:

1 Like

You could use an AlignPosition constraint. Give me a second I’ll make a quick demo for you

1 Like

demo.rbxl (30.2 KB)

The script is in StarterPlayerScripts

Hope it helps.

1 Like

It spins randomly when it gets near to the player

also, im pretty sure the first one doesn’t work and just goes to near spawn sometimes it works

(random spinning)
image
image

local plr = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DashEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("DashEvent")
local Dash_Normal = 20
local Dash_Timeout = 0.15
local Can_Dash = false
local char = script.Parent
local UIS = game:GetService("UserInputService")

local leftDashKey = Enum.KeyCode.A
local rightDashKey = Enum.KeyCode.D
local forwardDashKey = Enum.KeyCode.Q

local maxDelay = 0.25
local t1 = 0
local t2 = 0
local t3 = 0

local onlyW = game.ReplicatedStorage.OnlyW.Value

local CoolingDown = false

function DashForward(root,key)
	local character = root.Parent
	DashEvent:FireServer(character,root,tostring(key))
end

UIS.InputBegan:Connect(function (input, gp)
	local RootPart = char:FindFirstChild("HumanoidRootPart")
	if CoolingDown then return end
	if not RootPart then return end
	if gp then return end

	if input.KeyCode == leftDashKey then
		if tick() - t1 <= maxDelay and onlyW == false then
			CoolingDown = true

			DashForward(RootPart,"A")


			CoolingDown = false
		else
			t1 = tick()
		end
	end

	if input.KeyCode == rightDashKey then
		if tick() - t2 <= maxDelay and onlyW == false then
			CoolingDown = true

			DashForward(RootPart,"D")


			CoolingDown = false
		else
			t2 = tick()
		end
	end
	
	if input.KeyCode == forwardDashKey then
		if tick() - t3 <= maxDelay then
			CoolingDown = true

			DashForward(RootPart,"W")


			CoolingDown = false
		else
			t3 = tick()
		end
	end

end)

DashEvent.OnClientEvent:Connect(function(cg,root)
	while task.wait() do
		if cg and root then
			local alignPos, alignRot = cg:WaitForChild("AlignPosition"), cg:WaitForChild("AlignOrientation")
			local origin = cg.Position

			--//Offset in front of player's torso (relative to look direction)
			local targetPosition = (root.CFrame * CFrame.new(0, 0, -1)).Position

			--//Construct new CFrame to find the rotation the part should be set to
			local targetRotation = CFrame.lookAt(origin, targetPosition)

			--//Move object towards position in front of character's torso, and update rotation.
			alignPos.Position = targetPosition
			alignRot.CFrame = targetRotation
		end
	end
end)