Custom Camera Jitters at the Peak of Jump [SOLVED by me]

I made a topic like this around a week ago but I’m making another because 1) a new problem occurred from rewriting the script and, most importantly, 2) the topic was forgotten, like every other one of my topics on this type of situation, this time without even a single reply.

Okay, so at the peak of Sonic’s jump, the camera moves up a little bit for… whatever reason. Here’s a video:
robloxapp-20240510-2038334.wmv (1.7 MB)
and here’s the scripts, first of the Camera:

local state = "idle"

game:GetService("RunService").Heartbeat:Connect(function()
	if script.Parent.Parent.Sonic.Position.Y <= script.Parent.VerticalBottom.CFrame.Y then
		state = "down"
	elseif script.Parent.Parent.Sonic.Position.Y >= script.Parent.VerticalTop.CFrame.Y then
		state = "up"
	elseif math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalCenter.CFrame.Y) <= .8 then
		state = "idle"
	end
	
	if state == "down" then
		if script.Parent.Position.Y - math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalBottom.Position.Y) < script.Parent.Position.Y then
			if math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalBottom.Position.Y) <= 1.6 then
				script.Parent.CFrame -= Vector3.new(0,math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalBottom.Position.Y),0)
			else
				script.Parent.CFrame -= Vector3.new(0,1.6,0)
			end
		else
			state = "idle"
		end
	elseif state == "up" then
		if script.Parent.Position.Y + math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalTop.Position.Y) > script.Parent.Position.Y then
			if math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalTop.Position.Y) <= 1.6 then
				script.Parent.CFrame += Vector3.new(0,math.abs(script.Parent.Parent.Sonic.Position.Y - script.Parent.VerticalTop.Position.Y),0)
			else
				script.Parent.CFrame += Vector3.new(0,1.6,0)
			end
		else
			state = "idle"
		end
	end
end)

and second of Sonic:

local state = script.Parent.Variables.state
local speedY = script.Parent.Variables.speedY.Value
local speedX = script.Parent.Variables.speedX
local jumpForce = script.Parent.Variables.jumpForce.Value
local groundAngle = script.Parent.Variables.groundAngle
local direction = script.Parent.Variables.direction
local collisionBtmRight = script.Parent.Variables.collisionBtmRight
local collisionBtmLeft = script.Parent.Variables.collisionBtmLeft

local sonicSizes = {
	standing = Vector3.new(0.081, 3.31, 2.571),
	standingHitbox = Vector3.new(0.162, 2.731, 1.366),
	
	jumping = Vector3.new(0.08, 2.571, 2.571),
	jumpingHitbox = Vector3.new(0.162, 1.841, 1.366),
}
local sonicPics = {
	idleRight =  "rbxassetid://15036394678",
	idleLeft =   "rbxassetid://17228640834",
	
	walkRight1 = "rbxassetid://15036394489",
	walkRight2 = "rbxassetid://15036394334",
	walkRight3 = "rbxassetid://15036394169",
	walkRight4 = "rbxassetid://15036393971",
	walkRight5 = "rbxassetid://15036393740",
	walkRight6 = "rbxassetid://15036393489",
	
	jump1 = 	 "rbxassetid://17258635607",
}

wait(10)

script.Parent.Parent.btmLeftAnchorPoint.Touched:Connect(function(part)
	if part.Name == "Solid" and part.BrickColor == BrickColor.new("Institutional white") and part.CanCollide == true then
		collisionBtmLeft.Value = true
	end
end)

script.Parent.Parent.btmRightAnchorPoint.Touched:Connect(function(part)
	if part.Name == "Solid" and part.BrickColor == BrickColor.new("Institutional white") and part.CanCollide == true then
		collisionBtmRight.Value = true
	end
end)

script.Parent.Parent.btmLeftAnchorPoint.TouchEnded:Connect(function(part)
	if part.Name == "Solid" and part.BrickColor == BrickColor.new("Institutional white") and part.CanCollide == true then
		collisionBtmLeft.Value = false
	end
end)

script.Parent.Parent.btmRightAnchorPoint.TouchEnded:Connect(function(part)
	if part.Name == "Solid" and part.BrickColor == BrickColor.new("Institutional white") and part.CanCollide == true then
		collisionBtmRight.Value = false
	end
end)

--[[local function GetOutOfGround(part, obstacle)
	-- Get references to the parts you want to move
	local partToMove = part -- Change "Part1" to the name of your part
	local obstaclePart = obstacle -- Change "Part2" to the name of the part it may be colliding with

	-- Calculate the distance to move the part
	local distanceToMove = obstaclePart.CFrame.Y + obstaclePart.Size.Y / 2 + partToMove.Size.Y / 2 - partToMove.CFrame.Y

	-- Move the part instantly
	partToMove.CFrame = partToMove.CFrame + Vector3.new(0, distanceToMove, 0)
end--]]

local function GetOutOfGround(part, obstacle)
	-- Get references to the parts you want to move
	local partToMove = part -- Change "Part1" to the name of your part
	local obstaclePart = obstacle -- Change "Part2" to the name of the part it may be colliding with

	-- Calculate the distance to move the parts
	local distanceToMove = obstaclePart.Position.Y + obstaclePart.Size.Y / 2 + partToMove.Size.Y / 2 - partToMove.Position.Y

	-- Move Sonic instantly
	partToMove.Position = partToMove.Position + Vector3.new(0, distanceToMove, 0)

	-- Move attached parts (e.g., camera, side bars) by the same amount
	for _, attachedPart in ipairs(partToMove.Parent:GetChildren()) do
		if attachedPart:IsA("BasePart") and attachedPart.Name ~= "Sonic" and attachedPart.Name ~= "CamView" and attachedPart.Name ~= "LeftBar" and attachedPart.Name ~= "RightBar" then
			attachedPart.Position = attachedPart.Position + Vector3.new(0, distanceToMove, 0)
		end
	end
	for _, attachedPart in ipairs(partToMove:GetChildren()) do
		if attachedPart:IsA("BasePart") then
			attachedPart.Position = attachedPart.Position + Vector3.new(0, distanceToMove, 0)
		end
	end
end

--CONTROLS
game.ReplicatedStorage.Remotes.SonicJump.OnServerEvent:Connect(function()
	if state.Value == "Grounded" then
		speedY += jumpForce * math.cos(groundAngle.Value)
		--print(speedY)
		--speedX.Value += jumpForce	--* math.sin(ground angle)
		script.Parent.CFrame += Vector3.new(0, 0.1, 0)
		collisionBtmRight.Value = false
		collisionBtmLeft.Value = false
		
		script.Parent.Hitbox.Size = sonicSizes.jumpingHitbox
		script.Parent.Texture.Size = sonicSizes.jumping
		
		script.Parent.Texture.Texture.Texture = sonicPics.jump1
	end
end)
game.ReplicatedStorage.Remotes.SonicCutJump.OnServerEvent:Connect(function()
	print("blud cut his jump")
	if speedY > jumpForce / 1.5 then
		speedY = jumpForce / 1.5
	end
end)

game.ReplicatedStorage.Remotes.SonicMove.OnServerEvent:Connect(function(plr, directionInput)
	if directionInput == "Left" then
		direction.Value = "Left"
		script.Parent.Size = sonicSizes.standing
		script.Parent.Texture.Texture.Texture = sonicPics.idleLeft
	elseif directionInput == "Right" then
		direction.Value = "Right"
		script.Parent.Size = sonicSizes.standing
		script.Parent.Texture.Texture.Texture = sonicPics.idleRight
	end
end)

game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	local touchingBtmLeft = script.Parent.Parent.btmLeftAnchorPoint:GetTouchingParts()
	local touchingBtmRight = script.Parent.Parent.btmRightAnchorPoint:GetTouchingParts()

	local grounded = false

	--COLLISION DETECTION
	for i = 1, #touchingBtmLeft do
		if touchingBtmLeft[i].Name == "Solid" and touchingBtmLeft[i].BrickColor == BrickColor.new("Institutional white") and touchingBtmLeft[i].CanCollide == true then
			--print("touched left")
			collisionBtmLeft.Value = true
			GetOutOfGround(script.Parent, touchingBtmLeft[i])

			script.Parent.Hitbox.Size = sonicSizes.standingHitbox
			script.Parent.Texture.Size = sonicSizes.standing

			if direction.Value == "Left" then
				script.Parent.Texture.Texture.Texture = sonicPics.idleLeft
			elseif direction.Value == "Right" then
				script.Parent.Texture.Texture.Texture = sonicPics.idleRight
			end
		end
	end

	for i = 1, #touchingBtmRight do
		if touchingBtmRight[i].Name == "Solid" and touchingBtmRight[i].BrickColor == BrickColor.new("Institutional white") and touchingBtmRight[i].CanCollide == true then
			--print("touched right")
			collisionBtmRight.Value = true
			GetOutOfGround(script.Parent, touchingBtmRight[i])

			script.Parent.Hitbox.Size = sonicSizes.standingHitbox
			script.Parent.Texture.Size = sonicSizes.standing

			if direction.Value == "Left" then
				script.Parent.Texture.Texture.Texture = sonicPics.idleLeft
			elseif direction.Value == "Right" then
				script.Parent.Texture.Texture.Texture = sonicPics.idleRight
			end
		end
	end

	--CHECK IF GROUNDED
	if collisionBtmRight.Value == true then
		grounded = true
	else
		grounded = false
	end
	if collisionBtmLeft.Value == true then
		grounded = true
	else
		grounded = false
	end

	--SPEED Y
	if grounded == true then
		speedY = 0
	elseif grounded == false then
		speedY -= 0.0421875
	end

	--GROUND ANGLE
	if grounded == false then
		if groundAngle.Value > 0 then
			groundAngle.Value -= 2.8125
		elseif groundAngle.Value < 0 then
			groundAngle.Value += 2.8125
		end
	end

	--CHANGE STATE
	if grounded == true then
		state.Value = "Grounded"
	elseif grounded == false then
		if speedY > 0 then
			state.Value = "Jumping"
		elseif speedY == 0 then
			state.Value = "Grounded"
		elseif speedY < 0 then
			state.Value = "Falling"
		end
	end
	--print(grounded)

	--MOVE SPEED Y
	script.Parent.CFrame += Vector3.new(0, speedY, 0)
end)

Thanks.

great, let’s add this topic to the list of forgotten ones

1 Like

can you send a video, the video doesnt work

The wmv? Okay, I’ll send another one recorded with OBS

And I’ve just realized that I might need to rewrite this code for the third time, because horizontal movement (but I can just make a state 2), however the camera (in Sonic 1) may lock (for bossfights), it has camera bounds where the camera cannot move past and the screen may shake. Nice. But I’ll probably figure it out :slight_smile:

Rewrote the script myself once more, and finally it works.

oh, i love the devforum :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.