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

how to rotate your character torwards mouse ?

ok so, I pretty much completly remade robloxs first person perspective and i have everything made. i have the camera set perfectly but i cant get my character to rotate. i have been doing alot of research trying to figure out how to way to point the character toward the mouse, as of right now the only method i know is this

char.UpperTorso.CFrame = CFrame.new(char.UpperTorso.CFrame,rot) -- rot being the mouse 
-- position

the problem with this is if i put this in a heartbeat service its constantly updating the position of the player, so that leads to whenever the player walks it bugs. is there another method of rotating the player towards the mouse?

1 answer

✓ Accepted answer

yes u can do this:

place in LocalScript in StarterPack:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

plr.CharacterAdded:Connect(function(char)
	wait(.1)
	local root = char:FindFirstChild("HumanoidRootPart")
	
	game:GetService("RunService").RenderStepped:Connect(function()
		local dir = (root.CFrame:Inverse() * mouse.Hit).LookVector
		
		root.CFrame = root.CFrame * CFrame.Angles(0, -math.asin(dir.X), 0)
	end)
end)

if i had not met ur expectations comment me; i only read ur title

  • edit: an admin wanted me to expand on this and scene as i have nothing better to do as of now i will explain what dir does(although i did so in the comments dk if that counts)

dir, in essence is where the mouse is facing relative to the character(inversed but u can just take it as non inversed to get the picture better). Now since this is only where the mouse is facing, to convert it into rotational unit we need math.asin; picture it as i have marked a location where i need to turn towards, and math.asin is to change that into the degrees i have to turn to face said location.

7 comments

0

it works awesome but what exactly is dir getting? and what does math.asin do?

0

well

0

dir is essentially getting where the mouse is facing relative to the root

0

and asin is to transform that into rotational numbers

0

hm alright thanks alot. i would up you but im not a high enough level yet lol

0

lol ok

0

cant u just use a bodygyro

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