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

How can I adjust this camera script to change the character rotation as well?

I have made/adjusted this camera script to keep the player's mouse in the front of the screen. However, no matter how the character rotates (WASD or with the mouse) I want the character's backside to always be shown. I am working on making a shooter game so I don't want you to be able to shoot facing a different direction. I think what I might want to do is constantly update the character's body angle to the mouse rotation but I don't know how to go about that. Any suggestions/help? I will post the script I have below.


--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer

--Other
local Camera = game:GetService("Workspace").CurrentCamera

--------------
--- VALUES ---
--------------

local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(2.5,0.75,10)

----------------------
--- INITIALIZATION ---
----------------------

wait(1)
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

-----------------
--- FUNCTIONS ---
-----------------

userInputService.InputChanged:Connect(function(input)
	--if input.UserInputType == Enum.UserInputType.MouseMovement then
		xAngle = xAngle-input.Delta.x*0.4
		--print(xAngle-input.Delta.x*0.4)
		--Clamp the vertical axis so it doesn't go upside down or glitch.
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
	--end
end)

runService.RenderStepped:Connect(function()
	local Character = localPlayer.Character
	local rootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and rootPart then
		--Set rotated start cframe inside head
		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
		
		--Set camera focus and cframe
		local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
		
		Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
	end
end)

1 answer

This might help you.


local player = game.Players.LocalPlayer

local bg = Instance.new("BodyGyro",player.Character.HumanoidRootPart)
bg.P = 9e4
bg.maxTorque= Vector3.new(9e9,9e9,9e9)
bg.cframe = player.Character.HumanoidRootPart.CFrame

wait()

game:GetService("RunService").Stepped:connect(function()
bg.cframe = game.Workspace.CurrentCamera.CoordinateFrame * CFrame.Angles(-math.rad(0*50*1/150),0,0) 
end)

Hope this helped. Thanks.

1 comment

0

Just one problem. That's all I could think of and it won't stay FLAT on the floor. If you try it then you will know what I mean.

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