- Home /
Change camera on text click?
For my main menu of my game I thought it would be good if I had different 3D texts saying select mission, options and quit, and if you click one of them it changes do a different camera or changes the current camera to a different position showing more options. Can someone show me how to do this?
Answer by stingman · Sep 09, 2012 at 09:13 PM
Create a new camera. When you click your 3D Text to select one of your menu options Enable the Camera you want enabled and disable the other.
NGUI is a game user interface designed by tasharen entertainment. it's a plugin I use for creating GUIs. You can buy it on the asset store, or you can create your own GUI setup. Regardless, since you don't have NGUI you need to write your own code to handle turning the camera on and off. for example... if using a mouse and considering you are writing in C#:
using UnityEngine;
using System.Collections;
public class CameraChange : MonoBehaviour {
public Camera camera1;
public Camera camera2;
RaycastHit hit;
public GameObject text3D; // this is your 3d text object to click to turn off one camera and turn on the other. Make sure it has a collider on it.
void Start() {
camera1.enabled = true;
camera2.enabled = false;
}
void Update() {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject == text3D) {
camera1.enabled = false;
camera2.enabled = true;
}
}
}
}
}
I get an error saying unexpected token: Camera. I tried changing the script to
var Camera1 : GameObject;
var Camera2 : GameObject;
void OnClick() { // OnClick is representative of whatever code you create to handle your click
Camera.mainCamera.enabled = false;
Camera2.enabled = true;
}
but I got the error insert semicolon on line 4. Can you please help?
Sure thing, but do you even have NGUI? OnClick is a function provided in NGUI. Hence the reason I said that my example shows code from NGUI. Also, you are mixing javascript and C#. Another problem here. I wrote my example in C#. It should be pretty self explanatory but all you do is "enable" one camera and "disable" the other one. I suggest reading the Unity documentation a bit if you don't understand how the pseudo code I wrote for you works. It will only help you more in the long run. But the problem with my script above is that the variable camera2 is lowercase in the declaration and upper case in the function so I edited my post above to correct this. $$anonymous$$y bad :)
I am doing something similar switching between a top down map view to a close up battle camera for an encounter but I am using
battleCam.gameObject.SetActiveRecursively(true);
highCam.gameObject.SetActiveRecursively(false);
Is enabled a better way to do it than Set Active for any reason?
I don't know exactly how SetActiveRecursively cycles through game objects to deactivate/activate but I don't see the point in accessing all the other game objects/components if all you want to do is enable and disable one thing. If you can keep things simple why not? Unless it's more efficient for performance of course.
battleCam.enabled = true; highCam.enabled = false;
I understand the concept of enabling one camera and disabling the other but I dont know what NGUI actually is. Plus I have an error saying "A namespace can only contain types and namespace declarations" I'm confused, am I supposed to add to this script or something, please help?
Your answer
Follow this Question
Related Questions
Changing Camera 3 Answers
Changing Target Depending On Car Selected 1 Answer
throwing a ball camera position changes 0 Answers
how would i create a button that animates the main camera when clicked? 1 Answer
camera switching 2 Answers