Switching Cameras, Movement not working.
Hello!
I'm making a tower defense game for myself. Ideally I'd have two maps the player has to defend simultaneously (Area A & B), with the ability to switch between the two. My current concept consists of 4 cameras, 2 that render to the display and two that to render a special layer to a Radar. In theory, when the player is building on Area A, Area B's radar displays as picture in picture, then a CameraMaster that contains the CameraController script detects input to switch the view from A to B along with swaping UI's. I currently am able to switch between the two, but my CameraMovement script isn't detected and updated with the switch. I thought I could create some sort of bool check to see which camera is active, but I'm having trouble.
I was unable to use GetCompent<> to access the script to disable it on the camera, and was wondering how I could accomplish that. I'm also open to retooling the entire system.
public class CameraController : MonoBehaviour
{
public Camera cam1;
public Camera cam2;
public CameraMovement camMove1;
void Start()
{
cam1.enabled = true;
// and I want to access and swap the script.
// camMove1 = cam1.GetComponent<CameraMovement>;
// but my code is garbage
cam2.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
cam1.enabled = !cam1.enabled;
cam2.enabled = !cam2.enabled;
}
}
}
--------------------------------------------------------------------------------
public class CameraMovement : MonoBehaviour {
public float panSpeed = 30f;
public float panBorderThickness = 10f;
public float scrollSpeed = 5f;
public float minY = 10f;
public float maxY = 200f;
void Update () {
if (Input.GetKey("w") || Input.mousePosition.y >= Screen.height - panBorderThickness)
{
transform.Translate(Vector3.forward * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
{
transform.Translate(Vector3.back * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)
{
transform.Translate(Vector3.right * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
{
transform.Translate(Vector3.left * panSpeed * Time.deltaTime, Space.World);
}
float scroll = Input.GetAxis("Mouse ScrollWheel");
Vector3 pos = transform.position;
pos.y -= scroll * 1000 * scrollSpeed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, minY, maxY);
transform.position = pos;
}
}
Each camera has the camera movement script attached
I also encounter an error after about 20seconds of gameplay where my display gets disabled all together.
Answer by Skaster87 · May 25, 2017 at 05:20 AM
I added a singleton instance and solved all my issues, after discovering I was having issues with RayCast
public static CameraController instance;
public Camera cam1;
public Camera cam2;
private void Awake()
{
if (instance != null)
{
Debug.Log("two camera controllers");
}
instance = this;
}
void Start()
{
cam1.enabled = false;
cam2.enabled = true;
cam1.transform.gameObject.tag = "MainCamera";
}
to call a correct raycast with multiple cameras
RaycastHit hit;
ray = CameraController.instance.cam1.ScreenPointToRay(Input.mousePosition);