Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why wont this Swivel Around Mouse work?

So I'm trying to make a third person game, and have the Player swivel around their mouse like how Third Person MMO's do it sometimes. Here's what I have, but it doesn't do what I want it to do, I kind've just spin there constantly Code:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character; repeat wait() Character = Player.Character until Character;

function UpdateRotation()
	Character:SetPrimaryPartCFrame(Character.PrimaryPart.CFrame*CFrame.fromEulerAnglesXYZ(0, Mouse.X, 0))
end

while true do
	UpdateRotation()
	wait()
end

2 comments

0

Instead of character.PrimaryPart.CFrame use character:GetPrimaryPartCFrame()

0

Thank's, but that still doesn't change the fact that my guy is just spinning constantly

1 answer


local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character;
repeat wait() Character = Player.Character until Character;

local HRP = Character:WaitForChild("HumanoidRootPart")

function UpdateRotation()
	local to = Mouse.Hit.p -- get 3D position of where the mouse aims
	local pos = HRP.Position
	
	-- if to is (100,200,300) and pos is (5,5,5)
	-- it'll give us (100,5,300)
	local sameHeight = Vector3.new(to.X,pos.Y,to.Z)
	-- CFrame.new(pos,to) gives a CFrame at pos 'pos' aiming to pos 'to'
	-- because our Y is the same, the Torso stays straight up, but it'll
	-- be rotated towards the 'to' thing
	HRP.CFrame = CFrame.new(pos,sameHeight)
end

local RS = game:GetService("RunService").RenderStepped
while true do
	UpdateRotation()
	RS:wait()
end

This discussion is preserved as it was. New answers, comments, and votes are closed.