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:
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?
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
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)
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.
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!