A better "Character following mouse"?

In most of the third person shooter games on Roblox, they will include a “character following mouse” mechanic where the character rotates to where the mouse points at. This is a really common script I found in the toolbox, and I believe it’s being used by most of the people:

char.Torso.CFrame = CFrame.new(char.Torso.Position, Vector3.new(mouse.Hit.p.X, char.Torso.Position.Y, mouse.Hit.p.Z))

However, this ain’t the most reliable way to create this, the mouse will detect obstacles that the mouse is pointing at:
https://gyazo.com/cf391cb85ebbf5854e9f726101f2bba5

It also obstruct the movement of the character, such as the character walking slow when moving backwards, I don’t think hard setting it’s CFrame is a good approach, is there any better ways to do this?

4 Likes

One way could be to use UserInputService instead of the player Mouse.

The InputObject’s ‘Position’ property can give you the 2d position of the mouse pointer on the screen.
From that you create a Ray using
local ray = workspace.CurrentCamera:ScreenPointToRay(inputobject.X, inputobject.Y)

Then you could construct a cframe using that ray’s direction vector
char.Torso.CFrame = CFrame.new(char.Torso.Position, char.Torso.Position + ray.Direction)

Camera:ScreenPointToRay creates a unit ray starting at the camera’s position and pointing in the direction of the mouse. Parts will be ignored unless you use something like FindPartOnRay

1 Like

Is it possible to neglect the Y axis? It still look weird to me.
https://gyazo.com/5012fe6fd19db6a6d6f5740391d5033f

Oops, didn’t realize that.
char.Torso.CFrame = CFrame.new(char.Torso.Position, char.Torso.Position + ray.Direction * Vector3.new(1, 0, 1))

1 Like

Does it work cause I need it to. Also where do u put the script I wanna help fix it if it doesnt work

Thanks, it works well, but why is it solved by multiplying 1,0,1? By the way, is binding this to InputChanged the best way to do it?

Multiplying direction by (1,0,1) projects it onto the XZ plane, so it ignores the Y component of where your mouse is pointing.

And yes I would bind this to InputChanged.

I believe it is the best way and I actually don’t know why it was solved that way. You live you learn (EDIT: He answered that)

Thanks for the explanation! I also came up with a better binding, utilizing UserInputService:GetMouseLocation()

game:GetService("RunService").RenderStepped:Connect(function()

local A = uip:GetMouseLocation()

local ray = workspace.CurrentCamera:ScreenPointToRay(A.X, A.Y)

char.Torso.CFrame = CFrame.new(char.Torso.Position, char.Torso.Position + ray.Direction * Vector3.new(1, 0, 1))

end)
3 Likes

I tried it its not working am I supposed to put StarterPlayer, StarterCharacterScripts? Heres the code I added local ray = workspace.CurrentCamera:ScreenPointToRay(inputobject.X, inputobject.Y)

char.Torso.CFrame = CFrame.new(char.Torso.Position, char.Torso.Position + ray.Direction * Vector3.new(1, 0, 1))

I need this for my game and i tried it do I put it in StarterPlayer, StarterCharacterScripts

He is only showing a small part of his script. You need to obtain the ‘char’ from the LocalPlayer.Character and ‘inputobject’ from a UserInputService event.

ye I’ll do that now than thats why it was red sry

I found a free model weapon and it includes something similar to this too, it works well and doesn’t glitch or obstruct the character movement. Inside 3000 lines of code, I found this:

		local MDir = mouse.UnitRay.Direction.unit
		local RotTarget = Vector3.new(MDir.x,0,MDir.z)
		local Rotation = CFrame.new(char.Torso.Position,char.Torso.Position + RotTarget)
		BG.cframe = Rotation
		BG.P = 1e5

It utilizes a BodyGyro parented to HMR and set it’s CFrame, It also use mouse’s UnitRay, I don’t really know the internal reasons behind it, but it performs better!

Before; You can see the character is going slow when moving backwards and even walking animations playing when idle:
https://gyazo.com/80171d1364c3a6e792e5e2b840e17596

After; Everything goes smoother!
https://gyazo.com/e38e36f3803ac37f2dca49f2e808010e

3 Likes