# Attempting to rotate the player to face mouse pos. Resulting in strange behavior

Hello all. Recently I’ve been working on a script to get the player to rotate towards the mouse. I’ve had some trouble trying to understand what exactly is causing this behavior:

At first I utilized the roblox CFrame(pos1,pos2) constructor to set the players rotation and it resulted in the clip above. What’s bizarre is that the players mouse pointer should only dip on the y axis when going over that edge, with the X and Z axis staying unaffected.

``````FM.BodyGyro.CFrame = CFrame.new(
PrimPart.Position,
Vector3.new(Mouse.hit.p.X,PrimPart.CFrame.p.Y,Mouse.hit.p.Z)
)
``````

I then tried using a trigonometrey approach to it:

``````local ToPlayerSpace = PrimPart.Position - Mouse.hit.p

local Opposite = ToPlayerSpace.X

``````

But, this also yielded the same result. I’m very confused on what is causing this behaviour.

``````FM.BodyGyro.CFrame = CFrame.new(
PrimPart.Position,
Vector3.new(Mouse.hit.p.X,PrimPart.CFrame.p.Y,Mouse.hit.p.Z)
)
``````

the first parameter of the CFrame is a Vector3, so since you put a comma they assumed you’re using a second parameter. So, t fix it, try this:

``````FM.BodyGyro.CFrame = CFrame.new(
PrimPart.Position + Vector3.new(Mouse.hit.p.X,PrimPart.CFrame.p.Y,Mouse.hit.p.Z)
)
``````

i don’t believe this should be true.

consider this poorly drawn representation:

as you can see, the endpoints of the rays cast by the two cameras are different on the X and Y axis. in 3d, they could be different on all axis. Y isn’t the only thing changing here, which is why the player faces in the direction the ray hits void

Ah, right. Whoops. Anyway, what do you recommend I do to fix this? I had a friend tell me to cast a ray myself from the mouses 2d screen pos. But, that resulted in another issue. (It fixed the distance thing though)

``````local length = 300

local X = Mouse.X
local Y = Mouse.Y

local unitRay = cam:ScreenPointToRay(X, Y)

local mouse_pos = unitRay.Origin + unitRay.Direction * length

local ToPlayerSpace = mouse_pos - PrimPart.Position

PrimPart.CFrame = CFrame.new(PrimPart.CFrame.p) * CFrame.Angles(0, Radians ,0)
``````

The player rotates strangely now and I don’t quiet know what to do to fix this.

https://gyazo.com/20186f829a6c84755ebc0abee1df617e

I have found the solution. Instead of using mouse hit which is a raycast, I can just get the players primary parts screen position in pixels and the mouses screen position in pixels, make the mouse relative space to player pixel pos and then run the tangent math on that.

``````local X = Mouse.X
local Y = Mouse.Y

local Vector, OnScreen = cam:WorldToScreenPoint(PrimPart.Position)

local ToPlayerSpace = Vector2.new(Vector.X,Vector.Y) - Vector2.new(Mouse.X,Mouse.Y)