Can someone please look at the C# script below and let me know if it is right . i am not a programmer so it only a guess . I just what to turn the main camera on and off from the key board C and Z keys , if I can do it just using the C key even better .
Thanks
var Camera : Transform;
function Update(){
if (Input.GetMouseButtonDown(0)){
Camera.GetComponent(SmoothFollow1).enabled = false;
Camera.GetComponent(SmoothLookAt).enabled = true;
}
if (Input.GetMouseButtonDown(1)){
Camera.GetComponent(SmoothFollow1).enabled = true;
Camera.GetComponent(SmoothLookAt).enabled = false;
}
}
using UnityEngine;
using System.Collections;
public class CameraSwitch : MonoBehaviour {
void Update() {
if (Input.GetKeyDown(KeyCode.C))
Camera.Main Camera = false;
else
Camera.Main Camera = true;
if (Input.GetKeyDown(KeyCode.Z))
Camera.Main Camera = true;
else
Camera.Main Camera = false;
}
}
Sorry, apparently disabling the main camera seems to make Camera.main lose its reference.
This is working for me:
using UnityEngine;
public class CameraTest : MonoBehaviour
{
public Camera MainCamera;
void Awake()
{
MainCamera = Camera.main;//get a reference to the main camera
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
MainCamera.enabled = MainCamera.enabled ? false : true;
}
}
}
do you have second camera in the scene? if you turn off the main camera you will have a black screen unless you have a second camera to show HUD images or some UI buttons.
The script disables and enables the main camera as intended. The script can be attached to any GameObject but I’m completely lost as to what you want @Ruckrova . Please be more clear when trying to relay what you would like to happen in the script.
Maybe you meant disabling and enabling the SmoothFollow1 and SmoothLookAt components? If that is the case then you should use the code posted below. Attach it to any GameObject in your scene and assign the two components accordingly.
using UnityEngine;
public class CameraTest : MonoBehaviour
{
public SmoothFollow1 Follow;
public SmoothLookAt LookAt;
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
if (Follow.enabled)
{
Follow.enabled = false;
LookAt.enabled = true;
}
else
{
Follow.enabled = true;
LookAt.enabled = false;
}
}
}
}
using UnityEngine;
using System.Collections;
public class SwitchCam : MonoBehaviour {
public Camera MainCamera;
public Camera SecondCam;
void Awake()
{
MainCamera = Camera.main;//get a reference to the main camera
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.C))
{
MainCamera.enabled = MainCamera.enabled ? false : true;
SecondCam.enabled = SecondCam.enabled ? false : true;
}
}
}