- Home /
How can I set up cameras to track the player from room to room?
I've implemented player movement, as well as three different rooms. I understand how to create a first and third person camera, but how can I make three different camera views to track the player from room to room? More specifically, is there a way to zoom in on each room when the corresponding button is clicked?
So far I have a separate script attached to the main camera with this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraControls : MonoBehaviour
{
public GameObject player;
//private Vector3 offset;
public bool greenRoom;
public bool blueRoom;
public bool purpleRoom;
void OnGUI()
{
// Room 1 (Green room)
if (GUI.Button(new Rect(20, 50, 140, 40), "Green Room Camera"))
{
greenRoom = true;
blueRoom = false;
purpleRoom = false;
}
// Room 2 (Blue room)
if (GUI.Button(new Rect(20, 110, 140, 40), "Blue Room Camera"))
{
blueRoom = true;
greenRoom = false;
purpleRoom = false;
}
// Room 3 (Purple room)
if (GUI.Button(new Rect(20, 110, 140, 40), "Purple Room Camera"))
{
blueRoom = false;
greenRoom = false;
purpleRoom = true;
}
}
// Start is called before the first frame update
void Start()
{
//offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update()
{
if (greenRoom == true && blueRoom == false && purpleRoom == false)
{
}
if (blueRoom == true && greenRoom == false && purpleRoom == false)
{
}
if (purpleRoom == true && blueRoom == false && greenRoom == false)
{
}
}
}
Answer by sh_code · Jan 29, 2019 at 11:12 PM
triggers delineating which area/room should use which camera (position).
empty transforms as "placeholders" for the camera positions
script on triggers - if player enters this trigger/group of triggers, the assigned empty transform will be used as new position for main camera
that should be all.
You're welcome, have a nice day :)
So if I'm understanding, my booleans are the triggers which tell the script which camera to use. And I'm assu$$anonymous$$g that you're saying that I have to use "transform.position" in some way? Sorry to ask, but it really helps to see an example; would you able to show me how to do it for the green room? (the center of that room is just 0, 0) Thank you for your help!
no, forget the booleans, Triggers are Triggers - Colliders set to act as triggers
oh, don't do it in update, and don't do it on camera object. make that script so you can attach it to the trigger objects (google unity3d box trigger, and unity3d compound trigger/collider), and use their OnTriggerEnter event.
... but now i've made it too easy for you... O:-/
Hm, thanks, I'll go ahead and google those! I'm curious, is there a way to do it in the script that I have now or is using the trigger event the only way??
there's many ways to do anything. even learning. you think examples would help, i think (from experience) that sometimes just having the concept and needing to think on your own how to translate it into code is better ;)
there are at least three ways to do it in the script you already have, but you will still need the triggers anyway in at least 2 of those ways. and all of those ways are worse, more complicated, less clear, and less performant than what i suggested.
this is one of the few cases where i would dare to say that the way i suggested is objectively the best and most unity-like to solve this task ;)