How would I make the character snap towards the direction my mouse is looking?

Hey there! I’m making a 2D type of game, and I want the player look left or right, depending on where the mouse is pointing, but it’s not working at all, and just makes the character jitter really quickly. It detects if the mouse is on the right or left correctly, but the rotating isn’t working correctly.

local script:

local mHit = mouse.Hit
if mHit.Position.Magnitude > humanoidRootPart.Position.Magnitude then
	rotate2DEvent:FireServer("Left")
elseif mHit.Position.Magnitude < humanoidRootPart.Position.Magnitude then
	rotate2DEvent:FireServer("Right")
end

Server script:

rotate2DEvent.OnServerEvent:Connect(function(player, direction)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	
	if direction == "Left" then
		humanoidRootPart.Orientation = Vector3.new(0,-90,0)
	else
		humanoidRootPart.Orientation = Vector3.new(0,90,0)
	end
end)

Any way I can get this working? Any help is greatly appreciated!

3 Likes

magnitude is just the length of a vector, you’re comparing which position is farthest from the origin with your current code,essentially.

i would figure out what axis (x or z axis) your game is played on and compare the position of the mouse on that axis to the position of the rootPart on that axis to determine left/right.

I’ve done that, but that doesn’t fix the issue of the player jittering. It prints left/right correctly, so that’s not the problem.

do you have the autorotate property of the humanoid disabled?

Yes, I have AutoRotate disabled.

could you send a gif or video of the jitter? how is it only on the Y axis or does it jitter on the X and Z slightly?

https://streamable.com/aubw65

This is what it looks like.

from the clip it looks like when you set the orientation it also sets the player’s position to its last position as the server saw it. i would recommend instead of using remote events to rotate the player just handle it on the client.

1 Like

I can’t set the orientation on the client, is there a way I can use CFrame with it? If I just set angles, it will change it based on where the HumanoidRootPart is already rotated.

sorry for the late reply, however yes you can just set the CF of it. i’d pass the humanoidrootpart position to the server or set it on the client, make a CFrame from that position and find the radian or degree to multiply that CFrame by with CFrame.Angles on the Y axis to orient it.

1 Like

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