How do i rotate a dummy on the Y axis

So I’ve made a thingy that rotates the humanoidRootPart based on the last mouse x

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local LastMousePositionX = nil
local Sensitivity = 2
local Dummy = game.Workspace.bob

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if not GameProcessed and Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
		LastMousePositionX = Mouse.X
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
		LastMousePositionX = nil
	end
end)

UserInputService.InputChanged:Connect(function()
	if LastMousePositionX then
		local YOrientation = (Mouse.X - LastMousePositionX) / Sensitivity

		if YOrientation >= 180 then
			repeat
				YOrientation = YOrientation - 180
			until YOrientation <= 180
		end

		Dummy.HumanoidRootPart.Orientation = Vector3.new(0, YOrientation, 0)
	end
end)

It works but the dummy goes spiral for some reason and sometimes the dummy doesn’t move/rotates at all

Example:

help

Replace:

Dummy.HumanoidRootPart.Orientation = Vector3.new(0, YOrientation, 0)

with:

Dummy:SetPrimaryPartCFrame(CFrame.new(Dummy.HumanoidRootPart.Position) * CFrame.Angles(0, math.rad(YOrientation), 0))
2 Likes

Thank you ( I don’t know what any of that means )

Let me explain then. At first Dummy.HumanoidRootPart.Orientation = Vector3.new(0, YOrientation, 0) is not working because your are applying rotation only to signle part.

So to apply rotation (position as well) to whole model you need to use Model:SetPrimaryPartCFrame. Since this function requires CFrame you need to make one.

One of the CFrame’s constuctors is CFrame.new(Vector3 pos) which will make CFrame from Vector3 containing only position. To set orientation we need to modify this CFrame by multiplying it by CFrame containing orientation (CFrame.fromOrientation()). To make CFrame from orientation you need to provide orientation not in degrees but in radians. Since you are calculating orientation in degrees you need to convert it to radians by simply doing this: math.rad().

I’m not good at explaining stuff but i hope it helps just a bit!

2 Likes