How to switch camera view when player interacts with a specific area in a game?

Hi everyone!
2023-07-03 15-00-56.mkv (5.8 MB)


I would like to ask for your advice on how to achieve a specific effect in my game. When the player approaches a certain area and presses a button, the camera should switch to a specific position, and when the interaction is over, it should return to the original view. How can I achieve this effect?

Should I set up multiple cameras and switch between them when the player interacts, or is it better to use just one camera throughout the game? I am currently trying to use just one camera, but because I have used .lookat(player) in my code, it doesn’t feel quite right. I’m not sure how to modify it.

Thank you for your help!

my code
https://gotoo.co/demo/elizabeth/03_ElizaWorld/test/MovementAndJump.html

I think better use multiple cameras. Because for main camera maybe added some objects like sounds, lights etc.

You can use one camera but uses different pivots to lerp the camera into positions;

Try this code:

// Create cameras
window.createCameras=function(c0){
	engineParameters.camera.zoom=1
  engineParameters.cameras=[]
	if(c0)engineParameters.cameraIndex=1
	// Chase camera
	registerCamera(modelsParameters.driveVehicleIndex[playerParameters.index]!=null?.65:.86,modelsParameters.driveVehicleIndex[playerParameters.index]!=null?.5:orientation==0?.5:.86,0,c0)
	// First person camera
  registerCamera(modelsParameters.driveVehicleIndex[playerParameters.index]!=null?.65:.86,modelsParameters.driveVehicleIndex[playerParameters.index]!=null?.15:.015,1,c0)
	// Aerial camera
	registerCamera(orientation==0?.86:1,modelsParameters.driveVehicleIndex[playerParameters.index]!=null?4:orientation==0?1.2:2,2,c0)
	// Orbit camera
	registerCamera(orientation==0?.86:1,modelsParameters.driveVehicleIndex[playerParameters.index]!=null?4:orientation==0?1.2:2,2,c0)
	if(c0){
		setTimeout(function(){
			processMemory.cX0=lsRd('myCamera'+myUserID.uid)==null?playerParameters.camera.select:JSON.parse(lsRd('myCamera'+myUserID.uid)).index
			engineParameters.cameraIndex=processMemory.cX0==3?2:processMemory.cX0
			if(c0)hideMyMesh()
		},900)
	}
}
// Register camera
function registerCamera(ht0,ds0,iC0,c0){
	processMemory.cam=new THREE.Object3D()
	processMemory.cam.position.set(0,playerControlsParameters.height[playerParameters.index]*ht0,-playerControlsParameters.height[playerParameters.index]*ds0)
	if(true){
		playerControlsParameters.player.add(processMemory.cam)
	}else playerControlsParameters.player.children[iC0]=processMemory.cam
	engineParameters.cameras.push(processMemory.cam)
	ht0=ds0=iC0=c0=undefined
}
1 Like

If the NPC faces the character, you can move the camera to a specific position (probably you would like to use the player’s position as a reference) using gsap. The code would look something like this (mine is more for react-three-fiber, but you can get the idea:

const playerPos = new vector3(playerRef.current.position())

const offset = 1 // adjust

const cameraNpcDialogue = new Vector3(
playerPos.x,
PlayerPos.y + offset,
PlayerPos.z,
)

gsap.to(camera.position, {
x: cameraNpcDialogue.x,
y: cameraNpcDialogue.y,
z: cameraNpcDialogue.z,
})

camera.lookAt(npcRef.current.position)

This code would go inside a conditional when you trigger the NPC dialogue.