How to tween the players camera to a part when E is pressed

For example, when a player presses E on the cone, the cam will tween to the yellow part. I’m going for a “Press E to Inspect” mechanic.

image

1 Like

You need to use UserInputService and TweenService and CurrentCamera.CFrame

1 Like

here is a simple script

local players = game:GetService("Players")
local tweenService = game:GetService("TweenService")
local userInputService = game:GetService("UserInputService")

local player = players.LocalPlayer

local jumpPower = player.Character:WaitForChild("Humanoid").JumpPower
local WalkSpeed = player.Character.Humanoid.WalkSpeed

local camera = workspace.CurrentCamera

function tweenCamera(time , endingPosition)
	camera.CameraType = Enum.CameraType.Scriptable
	local tween = tweenService:Create(camera , TweenInfo.new(time) , {CFrame = endingPosition})
	tween:Play()
	tween.Completed:Wait()
end

function resetCamera()
	camera.CameraType = Enum.CameraType.Custom
	player.Character.Humanoid.JumpPower = jumpPower
	player.Character.Humanoid.WalkSpeed = WalkSpeed
end

userInputService.InputBegan:Connect(function(input , gameProcessed)
	if gameProcessed then return end
	
	if input.KeyCode == Enum.KeyCode.E then
		
		player.Character.Humanoid.JumpPower = 0
		player.Character.Humanoid.WalkSpeed = 0
		
		local endingPosition = CFrame.new() --change to your part's CFrame
		tweenCamera(.2 , endingPosition)
	end
	
end)
1 Like

I change CFrame.new() to the yellow block right?

yes change it to

part.CFrame
1 Like

and do i put the local script anywhere specific?

StarterGui is the best for camera

1 Like

alright it works perfectly, but I just need a way to return the cam to the player

use the resetCamera function, i gave it in the script.

1 Like

oh didn’t see that, I’ve put

if input.KeyCode == Enum.KeyCode.E then
			
			resetCamera()

but how would I make it so it returns cam when the player presses it after the cam’s CFrame has been set to the yellow part?

2 Likes

do this

local players = game:GetService("Players")
local tweenService = game:GetService("TweenService")
local userInputService = game:GetService("UserInputService")

local player = players.LocalPlayer

local jumpPower = player.Character:WaitForChild("Humanoid").JumpPower
local WalkSpeed = player.Character.Humanoid.WalkSpeed

local camera = workspace.CurrentCamera

local changedCframe = false

function tweenCamera(time , endingPosition)
	changedCframe = true
	camera.CameraType = Enum.CameraType.Scriptable
	local tween = tweenService:Create(camera , TweenInfo.new(time) , {CFrame = endingPosition})
	tween:Play()
	tween.Completed:Wait()
end

function resetCamera()
	changedCframe = false
	camera.CameraType = Enum.CameraType.Custom
	player.Character.Humanoid.JumpPower = jumpPower
	player.Character.Humanoid.WalkSpeed = WalkSpeed
end

userInputService.InputBegan:Connect(function(input , gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.E then

		if changedCframe then resetCamera() return end

		player.Character.Humanoid.JumpPower = 0
		player.Character.Humanoid.WalkSpeed = 0

		local endingPosition = CFrame.new() --change to your part's CFrame
		tweenCamera(.2 , endingPosition)
	end

end)
3 Likes