How to make my character face to where the mouse is pointing ignoring parts?

Hello everyone!

I’m faced with a problem that I’ve already done thousands of searches but none of them helped me

Issue:

When my character is moving and there is a part behind him, the camera turns to the part, I don’t know how this happens and I think it has to do with my code

Maybe changing the method I’m using to rotate the HumanoidRootPart?

Code: (resumed)

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Root = character:WaitForChild("HumanoidRootPart")
local m = LocalPlayer:GetMouse()

RunService.RenderStepped:Connect(function()
	local RootPos, MousePos = Root.Position, m.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)

Here is a video showing my problem:

Thanks for helping!

You could use Mouse.UnitRay.Direction

1 Like

Well this happens: (edited wmv)

This is my edit:

local RootPos, MousePos = Root.Position, m.Hit.Position

to

local RootPos, MousePos = Root.Position, m.UnitRay.Direction

You should add the direction to the current root position so the look vector is right

CFrame.new(RootPos, RootPos + Mouse.UnitRay.Direction)
1 Like

It worked but now he is rotating up and down

Well i just did this:

Root.CFrame = CFrame.new(Root.Position, Root.Position + Vector3.new(m.UnitRay.Direction.X, 0, m.UnitRay.Direction.Z))

now worked ignoring the parts around the character!

It worked thank you!

You can also use CFrame.lookAlong.

Code:

Root.CFrame = CFrame.lookAlong(Root.Position, Vector3.new(m.UnitRay.Direction.X, 0, m.UnitRay.Direction.Z))
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.