How would I go about creating an over the shoulder aim system?

I am trying to create a simple over the shoulder aim system in my game, but the one I am using does not work properly after the character dies. Can someone help me? I don’t really know scripting that much.

--BEFORE (sorry about the quality)

It works properly and the character turns with the character.

https://streamable.com/bcxfea

The character stays facing the direction when the button was last pressed.

--AFTER

https://streamable.com/7eqw4o

This is the code I used in the video that I created but it does not work properly after death.

--if player.CharacterAdded then
	userInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			UserGameSettings.RotationType = Enum.RotationType.CameraRelative
			
		end
	end)
	userInputService.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			
			UserGameSettings.RotationType = Enum.RotationType.MovementRelative
			
			
		end
	end)
	
	userInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			UserGameSettings.RotationType = Enum.RotationType.CameraRelative
			
		end
	end)
	userInputService.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			
			UserGameSettings.RotationType = Enum.RotationType.MovementRelative
			
			
		end
	end)
	
	
end


This is the code I’ve tried it doesn’t really make sense but I tried using bodygyro to follow where the camera is looking from a part of a script that make your head follow the camera.

-- local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local cam = workspace.CurrentCamera
local tweenService = game:GetService("TweenService")
local userInputService = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")
local Gyro = hrp.BodyGyro

userInputService.InputBegan:Connect(function(input)
	if input.UserInputType ==Enum.UserInputType.MouseButton1 then
		game:GetService("RunService").RenderStepped:Connect(function()
			local camDirection = hrp.CFrame:ToObjectSpace(cam.CFrame).LookVector
			Gyro.CFrame = CFrame.new(hrp.Position),camDirection.X,0,0)
		end)
		
	end
end)

Camera Script

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
Camera.FieldOfView = 90
--------------
--- VALUES ---
--------------

local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3.5,0,7.5)

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

wait(0.01)
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
		--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)

Try debugging your scripts with print() and make comments in your script telling you what does what. That’s all I can do without a computer hope it helps.

--this is a comment in Lua.. double divide is a c# comment lol
print("first print")
print("second print")
1 Like

Humanoid.CameraOffset (roblox.com)

Are you trying to have the camera follow the mouse a bit after you finished firing mouse1?

I don’t see why you would need to use mouse1 began and mouse1 ended you could just have it detect the mouse and update it accordingly.

Use heartbeat instead of RenderStepped for a more fluent action.
Good look developing!

1 Like

I know how to use camera offset. I am trying to create a script that make the character follow where the camera or cursor is pointing to like GTA or any third person shooter.

I am trying to make the character follow where the camera is pointing when the left mouse is pressed. Well that’s the only way I know how to get keys to do an action. Also I just realized that I had so many duplicate code that do the same thing

This thread might help you: Over-The-Shoulder Camera System

1 Like

If you want your character to follow mouse1 with mouse1 pressed then I suggest you don’t do this input wise: began/ended.

What I do suggest is updating the rotation when mouse1 was pressed(not began/ended) and keep updating rotation a bit after mouse1 was pressed.

1 Like

THANK YOU. It even works with the camera script

How would I do that? I don’t know how to do that?

You don’t know? Neither do I! This is problems all developers must face at some point, you don’t start from the top, you start at the bottom then work your way up(tedious/boring/annoying, yes it is).
FYI I don’t have a computer right now so I’m limited in helpfulness.

Ohhhhh, You mean mouse1 down ?

Yes check if mouse 1 was pressed not with the began and ended methods since it could break if you hold it down for to long.