Improving my OTS system - character turns when you bump into stuff

So basically if you walk into a part, your character will keep turning right. I talked to AllYourBlox about this and he said it was probably because there was a feedback loop (i.e My Character was setting my Camera’s orientation, then my camera was setting my character’s orientation).

Right now I feel like I’m hitting my head against a wall to fix this lol, I thought I’d post my code here and see if you guys could help!

If I’ve neglected some important code form below, please let me know!

I’ll leave a link at the bottom so you can see it in action


function GetPopperPosition(Head, NextCamPosition)
	local ray = Ray.new(Head.CFrame.p, (NextCamPosition - Head.CFrame.p).unit * 100)
	local _, HitPosition = workspace:FindPartOnRayWithIgnoreList(ray, CharacterStatus.IgnoreList)
	
	return Head.Position:lerp(HitPosition, .8)
end

function AdjustForMouseDelta()
	local X = 0
	
	if MouseDelta then
		X = MouseDelta.X/150
		if Y-MouseDelta.Y/150 < 1 and MouseDelta.Y < 0 then
			Y = Y-MouseDelta.Y/150
		elseif Y-MouseDelta.Y/150 > -1 and MouseDelta.Y > 0 then
			Y = Y-MouseDelta.Y/150
		end
		
		Y = math.clamp(Y, -1, 1)
		MouseDelta = nil
	end
		
	return X
end

RenderStepped = function(Character, Humanoid)
	if (not Character:FindFirstChild("Head")) or (not Character:FindFirstChild("HumanoidRootPart")) then
		return
	end
			
	local HRP = Character.HumanoidRootPart	
			
	local X = AdjustForMouseDelta()
	HRP.CFrame = HRP.CFrame * CFrame.fromEulerAnglesXYZ(0,-X,0) 

	local PositionBehindSubject = (HRP.CFrame * CFrame.fromEulerAnglesXYZ(Y,0,0) * CFrame.new(0, 0, CurrentZOffset) ).p
	local TargetPosition = Vector3.new(PositionBehindSubject.X, PositionBehindSubject.Y, PositionBehindSubject.Z)
			
	local CurrentXOffset = Lerp(1, OTSXOffset, CurrentZOffset / OTSZOffset)
 	local TargetCFrame = CFrame.new(TargetPosition, HRP.CFrame.p) * CFrame.new(CurrentXOffset, 3, 0)
		
	TargetCFrame = Camera.CFrame:lerp(TargetCFrame, .5) 
		
	local PopperPosition = GetPopperPosition(Character.Head, TargetCFrame.p)
		
	if (HRP.CFrame.p - TargetPosition).magnitude > (HRP.CFrame.p - PopperPosition).magnitude then
		TargetCFrame = CFrame.new(PopperPosition, PopperPosition + TargetCFrame.lookVector)
	end	
		
	Camera.CFrame = TargetCFrame
		
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
end;

Press C to turn it on

1 Like

what I suppose happens, is that
when you run into a part, due to the running animation, your hrp (humanoidrootpart) would align towards the wall
since the camera is based on hrp, it would also rotate

I have had a camera that works exactly like this for two years, this is not a new thing but it has not always been the case. I believe it has only started being the case around five months ago. I haven’t yet had the time to dedicate the resources to solving it, but the issue is that the HumanoidRootPart is trying to align itself towards the wall (this is the recent change).

1 Like

Any idea how to stop this behaviour? AutoRotate is set to false.

This code is ran on init.

Humanoid.AutoRotate = false
			
Camera.CameraType = Enum.CameraType.Scriptable
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
			
Y = 0
			
pcall(function() GameSettings.RotationType = Enum.RotationType.CameraRelative end)
			
CharacterStatus.OTS = true

if you are trying to use RotationType.CameraRelative as a solution, aren’t you supposed to have AutoRotate set to true?

Old solution. CameraRelative doesn’t fix the problem when AutoRotate is turned on. Should delete, thanks!