[Help] Player Rotation using BodyGyro

Hello,

I want to make the player rotate towards the mouse using a body gyro. So what would I need to set the gyro’s Cframe to make it rotate towards the players mouse only on the Y rotation.

I have done a lot of research but I could not find any working formula so if there is one already can you send its link.

Any help is appritiated

1 Like

You can set the CFrame:

CFrame.new(position, mouse.Hit.p)

No but that would make the player look towards it on all axises and I need to use body gyro

Accually I have an idea maybe I can get a cframe by doing:

Local Cframe = CFrame.new(PrimaryPart.Position, mouse.hit)

And multiply its angles by 0,1,0 so that it will only affect the Y rotation

I cant test it right now but when I do I will notify

By the way, use the new CFrame.lookAt() function instead. You could just get the direction vector, and then flatten it to 2D space, and then use it to create a rotation CFrame:

local direction = mouse.Hit.Position - humanoidRootPart.Position
local flattenedDirection = (direction * Vector3.new(1, 0, 1)).Unit
local rotationCFrame = CFrame.lookAt(Vector3.new(), flattenedDirection)

No I accually made a simpler formula it is a bit similar to yours but its simpler

I cant open studio right now so when I do I will put it here:

The way it works is I just got the direction by subtracting torso position from mouse hit position and multiplying it by 1, 0, 1

And to get the cframe I wanted I just basically did CFrame.new(torso position, torso.position + direction)

And that works very well

Actually you don’t need the torso position for BodyGyros, since it just uses the rotational data of CFrame. And from my reply, I don’t need to .Unit the multiplied vector. So basicly the final code is this:

local direction = mouse.Hit.Position - humanoidRootPart.Position -- Get direction
direction *= Vector3.new(1, 0 ,1) -- Flatten direction
bodyGyro.CFrame = CFrame.lookAt(Vector3.new(), direction) -- Turn into CFrame

or a one-liner:

bodyGyro.CFrame = CFrame.lookAt(Vector3.new(), (mouse.Hit.Position-humanoidRootPart.Position) * Vector3.new(1,0,1))
2 Likes

Eh mine works since I already implemented it but thanks maybe I will use this another time or other people may use it

1 Like