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

Help on Camera Manipulation: Why does this happen?

My Script:

local gui = script.Parent

local player = game.Players.LocalPlayer 

local cam = game.Workspace.Camera

local distance = 10

local p = game.Workspace:FindFirstChild(player.Name)

local UIS = game:GetService("UserInputService")

local check = false
	
script.Parent.Menu2.Training.MouseButton1Click:Connect(function()
	p.LowerTorso.CFrame = CFrame.new(0, 1066.35+5, 31.286)
	p.LowerTorso.Anchored = false
	focushere = game.Workspace:FindFirstChild(player.Name).focus
	cam.CameraSubject = focushere
	p.Humanoid.Jump = true
	p.LeftFoot.Touched:Connect(function()
		if check == false then
			cam.CameraType = "Fixed"
			script.parent.Menu2.Visible = false
			script.parent.Background.Visible = false
			game.Players.LocalPlayer.CameraMaxZoomDistance = distance
			game.Players.LocalPlayer.CameraMinZoomDistance = distance
			cam.CFrame = CFrame.new(9.9973382949829,focushere.CFrame.Y,focushere.CFrame.Z) -- z = 27.442066192627 y = 
			--cam.CoordinateFrame = focushere.CFrame * CFrame.new(8,0,0) --* CFrame.Angles(0,0,math.rad(2)) 
			check = true
			p.Check.Value = true
		else
			return
		end
	end)
end)

repeat
	wait()
until gui.Startgame.Visible == false and gui.Menu.Visible == false and gui.Menu2.Visible == false


function rotate()
	local root_part_one = game.Workspace:FindFirstChild(player.Name).HumanoidRootPart
	local root_part_two = game.Workspace.StarterCharacter.HumanoidRootPart
 
	-- Face player 1 towards player 2
	root_part_one.CFrame = CFrame.new(root_part_one.Position, root_part_two.Position)
 
	-- Face player 2 towards player 1
	root_part_two.CFrame = CFrame.new(root_part_two.Position, root_part_one.Position)
end

function follow()
	--cam.CoordinateFrame = cam.CoordinateFrame * CFrame.new(1,0,0)-- * CFrame.Angles(0,0,math.rad(2))
	cam.CFrame = CFrame.new(9.9973382949829,focushere.CFrame.Y,focushere.CFrame.Z)
end

check3 = true

game:GetService("RunService").RenderStepped:Connect(function()
	p.Humanoid.Jumping:Connect(function()
		check3 = false
	end)
    if(check3 == true) then
        if(p.Humanoid.Health > 0) then
            --rotate();
            follow()
        end
	else
		check3 = true
	end
end)

Problem: Why is it that when I try setting Y and Z values of the Camera's CFrame to the Y and Z values of focus, the camera just faces forward? What I hope to happen is for the camera to face the right side of my player similar to that of a 2D game. Anyone have any clue what I did wrong or have a better solution to this? If so please help!

Clarification: focus and focushere is the same thing, Its just a part I place in front of my player's head to be the Camera's focus.

1 answer

✓ Accepted answer

Well, if you want the camera to just face to the side of the player, you can use the following in a LocalScript

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
camera.CameraType = "Fixed"
local character = player.Character or workspace:WaitForChild(player.Name)

function cameraUpdate()
	local root = character:WaitForChild("HumanoidRootPart")
	camera.CFrame  = CFrame.new(root.CFrame.X, root.CFrame.Y + 4, 12)
end
 
game:GetService("RunService").RenderStepped:Connect(cameraUpdate)

The Z-Output is the offset from the character to the side, and the "Fixed" style is meant to make sure the camera cannot change angles (the render stepped will automatically move the camera anyway)

However, in relation to fixing your script, on lines 28 and 56, you have the following line of code (i rounded the 9.997...)

cam.CFrame = CFrame.new(10, focus.CFrame.Y, focus.CFrame.Z)

You cannot call the Y and Z positions of a CFrame, so you need to call them with the Position Vector as shown below.

cam.CFrame = CFrame.new(10, focus.Position.Y, focus.Position.Z)

1 comment

0

You can get position from CFrame using CFrame.p which returns the position in Vector3, then CFrame.p.x for example

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