Skip to content

Instantly share code, notes, and snippets.

@igolden
Created October 18, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igolden/9565e53df1ac91c114f67a6e2b5ecce3 to your computer and use it in GitHub Desktop.
Save igolden/9565e53df1ac91c114f67a6e2b5ecce3 to your computer and use it in GitHub Desktop.
Main Camera toggle in Unity C# - toggles between two camera transforms
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraHandler : MonoBehaviour {
// Represents the main camera
GameObject mainCamera;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
// Target your main camera
mainCamera = GameObject.Find("Main Camera") as GameObject;
// Conditionally handle position changing
if (mainCamera.transform.position.z == 0)
{
mainCamera.transform.position = new Vector3(0, 3, -2);
// optional rotation
// mainCamera.tranform.rotation.x = -15;
} else
{
mainCamera.transform.position = new Vector3(0, 0, 0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment