Help with part that when touched moves your Camera

Hi,
The idea of this project I’m making is that when you touch this circle it moves your Camera to another part called Camera inside of a garage I built and there you can choose what car you buy. Like this from the game Driving Empire:


This is the Circle you need to touch in order to have your camera brought to the shop

This is a picture of the explorer in the project:
image
And this is a picture of the location and some stats of the Camera Part:
image

I have tried looking for solutions on the internet but could not find many helpful and I do not have much scripting experience in fact I am still learning so could someone please help. (also if possible could it be so that it does not teleport the camera there instead it just moves it).
Thanks

1 Like

I would use:
https://developer.roblox.com/en-us/api-reference/function/Camera/Interpolate

This function tweens the Camera in a linear fashion towards a new Camera.CFrame and Camera.Focus over a given duration.

Example:

local Camera = workspace.CurrentCamera
Camera.CameraType = "Scriptable"
Camera.CFrame = original cframe -- Change

Camera:Interpolate(newCFrame , Focus, seconds) -- Change
1 Like

Interpolate is deprecated. Shouldn’t he use TweenService instead?

1 Like

Although would this make it to that when the character touches the part it moves the Camera to a different spot? Also my scripting knowledge is very low and I am rather learning Lua so I don’t know what Tweens are or how to make a script.

1 Like

You just have to use a touched event to make this happen.
This has to be in a local script by the way.

local part = where the part is

part.Touched:Connect(function()
end)

oh ok, and in the moving the camera space I would put the other script you sent?

Yes, and just change the stuff that you need to fill in.

Ok, also What is the Focus thing? Also, this is the script I replaced the things you said and put it together but it still does not work.

local part = -5.413, 0.55, 29.127

local Camera =

workspace.CurrentCamera

Camera.CameraType = “Scriptable”
Camera.CFrame = -5.409, 1.453, 29.565 – Change

Camera:Interpolate(-80.151, 18.5, 38.772 , 3, 3) – Change

part.Touched:Connect(function()
end)

Before learning all of this, I would advise learning the basics of lua. Also, I did not realize that this was deprecated.
So as @1000knives said, you probably want to use TweenService:

Don’t forget to do: Camera.CFrame = CFrame.new(–cordinates here)

maybe you were just in a hurry and forgot to type it out when you were answering. If not then make sure to do that!

ok thanks, I will try that out.

1 Like

This is the script I put I think it should match what you said.
But it still does not work. Am I doing something wrong?
Camera.CameraType = “Scriptable”

Camera.CFrame = -5.409, 1.453, 29.565 – Change

Camera:Interpolate(-80.151, 18.5, 38.772 , 3, 3) – Change

Camera.CFrame = CFrame.new(-80.151, 18.5, 38.772 , 3, 3)

part.Touched:Connect(function()

end)

try that. Remember to always do CFrame.new() when you want to change the Cframe of an object

It still does not work could it be that I have done something wrong in the explorer?
image

1 Like

As @ RichardPRoosevelt said you need to use a localscript here if you are changing the camera. You can’t change camera from a server script

So you need to create a local script and put it somewhere that will run, put it in starterplayerscripts for example

Start off doing stuff simple, so first step, make it so that the camera just changes. Once you’ve got that working then add in fancier stuff like making it move smoothly, always easier to do things one little step at a time

So the script should be something like. Note if you are scripting stuff it makes it easier to not have spaces in the part names, so I’d suggest change the name of the part to TouchToBuyPart not “Touch To buy Part”

  local part = game.Workspace.TouchToBuyPart
  local camera = game.Workspace.CurrentCamera

    part.Touched:Connect(function()
      camera.CameraType = Enum.CameraType.Scriptable
      -- Change the numbers here to be the position you want the camera to be in
      camera.CFrame = CFrame.new(50, 10, 50)
    end)

This isn’t going to be the finished script yet, but should get the start of it working

1 Like

Ok it worked thank you so much, just how do I kind of like animate it so that it moves smoothly?

Ah great!

OK the animation is fairly easy to add in, you do want to use TweenService for that

local TweenService = game:GetService("TweenService")
local part = game.Workspace.TouchToBuyPart
local camera = game.Workspace.CurrentCamera

part.Touched:Connect(function(hit)
   -- Touched can get triggered lots of times, so check here if the camera is already scriptable, only do stuff if its not
  if camera.CameraType ~= Enum.CameraType.Scriptable then
    camera.CameraType = Enum.CameraType.Scriptable

    local targetPosition = Vector3.new(50, 10, 50) -- put the position of where you want the camera to be here
    local targetLookAt = Vector.new(60, 5, 50) -- put where you want the camera to look at here

    local targetCFrame = CFrame.new(targetPosition, targetLookAt)

    local tweenInfo = TweenInfo.new(1)  -- animation lasts 1 second

    local goal = {}
    goal.CFrame = targetCFrame

    local tween = TweenService:Create(camera, tweenInfo, goal)

    tween:Play()

    -- I'd also just move the player a bit so that they are no longer touching the part, otherwise when they go back from the shop they might end up getting teleported straight back when they move, which is annoying.  
    hit.Parent:SetPrimaryPartCFrame(hit.Parent.PrimaryPart.CFrame + Vector3.new(-5, 0, 0))

  end
          
end)

Somewhere you’re going to need a GUI button in your shop to exit and go back to normal, when you have that when it’s clicked you should just need to do

local camera = game.Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Custom

and it should jump back to the normal camera

Ok, I put that code inside of the same script as the one used to teleport the camera it still does teleport the camera just that it does not smoothly move it, it is the same as before. I might have done something wrong I think.
local part = game.Workspace.TouchToBuyPart
local camera = game.Workspace.CurrentCamera

part.Touched:Connect(function()
camera.CameraType = Enum.CameraType.Scriptable
– Change the numbers here to be the position you want the camera to be in
camera.CFrame = CFrame.new(-63.778, 8.25, 46.251)

local TweenService = game:GetService("TweenService")
local part = game.Workspace.TouchToBuyPart
local camera = game.Workspace.CurrentCamera

part.Touched:Connect(function(hit)
	-- Touched can get triggered lots of times, so check here if the camera is already scriptable, only do stuff if its not
	if camera.CameraType ~= Enum.CameraType.Scriptable then
		camera.CameraType = Enum.CameraType.Scriptable

		local targetPosition = Vector3.new(-63.778, 8.25, 46.251) -- put the position of where you want the camera to be here
		local targetLookAt = Vector.new(-66.307, 5.447, 19.599) -- put where you want the camera to look at here

		local targetCFrame = CFrame.new(targetPosition, targetLookAt)

		local tweenInfo = TweenInfo.new(3)  -- animation lasts 1 second

		local goal = {}
		goal.CFrame = targetCFrame

		local tween = TweenService:Create(camera, tweenInfo, goal)

		tween:Play()

		-- I'd also just move the player a bit so that they are no longer touching the part, otherwise when they go back from the shop they might end up getting teleported straight back when they move, which is annoying.  
		hit.Parent:SetPrimaryPartCFrame(hit.Parent.PrimaryPart.CFrame + Vector3.new(-5, 0, 0))

	end

end)

end)
Is this correct?
also thank you for the thing about resetting the Camera it is just what I needed.

Just to double check, make sure that you don’t have this in the script anywhere, so if that’s there take it out and just have the code that’s doing the movement by tween?
It should all be smooth then.

So the code in the script should I think be just this; nothing else

local TweenService = game:GetService("TweenService")
local part = game.Workspace.TouchToBuyPart
local camera = game.Workspace.CurrentCamera

part.Touched:Connect(function(hit)
	-- Touched can get triggered lots of times, so check here if the camera is already scriptable, only do stuff if its not
	if camera.CameraType ~= Enum.CameraType.Scriptable then
		camera.CameraType = Enum.CameraType.Scriptable

		local targetPosition = Vector3.new(-63.778, 8.25, 46.251) -- put the position of where you want the camera to be here
		local targetLookAt = Vector.new(-66.307, 5.447, 19.599) -- put where you want the camera to look at here

		local targetCFrame = CFrame.new(targetPosition, targetLookAt)

		local tweenInfo = TweenInfo.new(3)  -- animation lasts 1 second

		local goal = {}
		goal.CFrame = targetCFrame

		local tween = TweenService:Create(camera, tweenInfo, goal)

		tween:Play()

		-- I'd also just move the player a bit so that they are no longer touching the part, otherwise when they go back from the shop they might end up getting teleported straight back when they move, which is annoying.  
		hit.Parent:SetPrimaryPartCFrame(hit.Parent.PrimaryPart.CFrame + Vector3.new(-5, 0, 0))

	end

end)