Proximity prompt to change camera

I am trying to make a telescope which you can hold E on a proximity prompt and it will transport your camera to ‘space’ and show you some celestial bodies. This script should just transport your camera to a new location (actual location would be implemented when I know it works) and you’d then be able to press E, or a gui button to return your camera back to normal. This does however not work.

local part = script.Parent 


local function teleportCamera(player)
    local character = player.Character
    if character then
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
        local camera = player.Camera
        if camera then
            camera.CameraType = Enum.CameraType.Scriptable
            camera.CFrame = CFrame.new(0, 0, 0)
        end
    end
end


local proximityPrompt = script.Parent
proximityPrompt.Enabled = true


proximityPrompt.Triggered:Connect(function(player)
    teleportCamera(player)
end)

Locations:
image

Camera is client only. You’ll need to implement this on a local script.

1 Like

New Locations:
image

It still does not work though.

local camera = workspace.CurrentCamera
local localPlayer = game:GetService("Players").LocalPlayer

local part = workspace.Part
local proximityPrompt = part.Attachment.ProximityPrompt

local function teleportCamera()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = CFrame.new(0,10,0)
	-- ...
end

proximityPrompt.Triggered:Connect(teleportCamera)
3 Likes

It works! Thank you.
What do you think would be a good way to let the player know how to exit the camera view? I’m not that great at UI as my UI always breaks depending if you’re on mobile or not. Also, how do I add a way to exit the camera view?

Any time!

I’d add a simple button. For mobile compatibility, you could have different events connected to the same button. .TouchTap, .MouseButton1Click

TextButton: TextButton | Documentation - Roblox Creator Hub (particularly events inherited from GuiObject and GuiButton).

Alternatively, you could add a key bind and a button for mobile with ContextActionService.

CAS: Mobile | Documentation - Roblox Creator Hub

I think I’d go with the first approach and possibly add a key shortcut, because even PC players may look for a button to click with a mouse.

1 Like

Is it possible that any kind of key input or screen input would reset the camera afterwards? From the moment they press any key / click their mouse / tap their screen?

1 Like

Once camera is scriptable, it’s in your control. In the camera modules, no controllers will be active. So a scriptable camera + CFrame change is sufficient. At least it always was in my projects.

1 Like

Very confusing stuff for a first time using camera control but I’ll check it out when I’m back online tomorrow!

All good. Soon you’ll see it’s rather simple. 1. CameraType → when scriptable, the control is ours; 2. CameraSubject → what the camera is focused on; 3. CFrame → while scriptable, it’s on us to update the position. In your case, it’s static, so just one set of a CFrame with the desired direction. For instance,

camera.CFrame = CFrame.lookAt(newCameraPosition, thePositionCameraIsLookingAt)

If you have any additional questions tomorrow, my DMs are open. Good luck!

1 Like

I noticed that when I was in the view, the proximity prompt stays, wouldn’t it be possible that when you hold E you can’t move anymore (so that you can’t walk away from the prompt and get stuck in the view) and then when you hold E again, you exit?

Absolutely, you can keep the player on the spot. Anchoring the humanoid root part would work, although I believe setting JumpHeight and WalkSpeed, and later restoring the values back to the original is quite neat for this use case.

The proximity prompt probably stays on the screen because it is on the view port and the humanoid stays in range. Sure, that works, although I prefer ContextActionService or button sollution.

1 Like

I have modified the script to make your player not moveable anymore when you hold e

I have also added a button at the location which if you press makes your camera go back to your player, though you are not able to move yet.

local camera = workspace.CurrentCamera
local localPlayer = game:GetService("Players").LocalPlayer

local part = workspace.Telescope.Prox
local proximityPrompt = part.Attachment.ProximityPrompt

local function teleportCamera()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = CFrame.new(-31.618, -306.795, 359.889)

	-- Disable character movement
	local humanoid = localPlayer.Character.Humanoid
	humanoid:ChangeState(Enum.HumanoidStateType.Physics)
end

proximityPrompt.Triggered:Connect(teleportCamera)

local clickDetector = workspace.Back.ClickDetector

local function returnCamera()
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = localPlayer.Character.Humanoid
	camera.CFrame = localPlayer.Character.HumanoidRootPart.CFrame

	-- Enable character movement
	local humanoid = localPlayer.Character.Humanoid
	humanoid:ChangeState(Enum.HumanoidStateType.None)
	humanoid:ChangeState(Enum.HumanoidStateType.Physics)
end

clickDetector.MouseClick:Connect(returnCamera)

EDIT:
I have removed the no more character movement from the script since i dont need it anymore. This script makes my view change when i hold e and teleports my camera back when i click something.

local camera = workspace.CurrentCamera
local localPlayer = game:GetService("Players").LocalPlayer

local part = workspace.Telescope.Prox
local proximityPrompt = part.Attachment.ProximityPrompt

local function teleportCamera()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = CFrame.new(-31.618, -306.795, 359.889)

end

proximityPrompt.Triggered:Connect(teleportCamera)

local clickDetector = workspace.Back.ClickDetector

local function returnCamera()
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = localPlayer.Character.Humanoid
	camera.CFrame = localPlayer.Character.HumanoidRootPart.CFrame

end

clickDetector.MouseClick:Connect(returnCamera)

Thank you for your help!

2 Likes

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