-3
好了,所以我有2個攝像頭設置在我的層次在Unity:如果我按下某個鍵,我怎麼才能在相機之間切換?
我想知道,當在遊戲中,我怎麼能這兩款相機之間切換,當某個鍵被按下?我知道我可能需要爲此做一個腳本,只是不知道我該怎麼做。
好了,所以我有2個攝像頭設置在我的層次在Unity:如果我按下某個鍵,我怎麼才能在相機之間切換?
我想知道,當在遊戲中,我怎麼能這兩款相機之間切換,當某個鍵被按下?我知道我可能需要爲此做一個腳本,只是不知道我該怎麼做。
您可以添加多臺攝像機
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
// Use this for initialization
public Camera[] cameras;
private int currentCameraIndex;
// Use this for initialization
void Start() {
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i=1; i<cameras.Length; i++)
{
cameras[i].gameObject.SetActive(false);
}
//If any cameras were added to the controller, enable the first one
if (cameras.Length>0)
{
cameras [0].gameObject.SetActive (true);
Debug.Log ("Camera with name: " + cameras [0].GetComponent<Camera>().name + ", is now enabled");
}
}
// Update is called once per frame
void Update() {
//If the c button is pressed, switch to the next camera
//Set the camera at the current index to inactive, and set the next one in the array to active
//When we reach the end of the camera array, move back to the beginning or the array.
}
public void Change()
{
currentCameraIndex ++;
Debug.Log ("C button has been pressed. Switching to the next camera");
if (currentCameraIndex < cameras.Length)
{
cameras[currentCameraIndex-1].gameObject.SetActive(false);
cameras[currentCameraIndex].gameObject.SetActive(true);
Debug.Log ("Camera with name: " + cameras [currentCameraIndex].GetComponent<Camera>().name + ", is now enabled");
}
else
{
cameras[currentCameraIndex-1].gameObject.SetActive(false);
currentCameraIndex = 0;
cameras[currentCameraIndex].gameObject.SetActive(true);
Debug.Log ("Camera with name: " + cameras [currentCameraIndex].GetComponent<Camera>().name + ", is now enabled");
}
}
}
非常基本的問題,你應該去一些C#教程。
無論如何,這是可以做到的。把這個更新方法:
if (Input.GetKeyDown("space"))
{
//don't forget to set one as active either in the Start() method
//or deactivate 1 camera in the Editor before playing
if (Camera1.active == true)
{
Camera1.SetActive(false);
Camera2.SetActive(true);
}
else
{
Camera1.SetActive(true);
Camera2.SetActive(false);
}
}
這可以幫助:http://answers.unity3d.com/questions/63221/how- to-set-main-camera.html – Keiwan
通過簡單地等待某人爲您寫腳本,您不會學到任何東西。你需要有一些東西。至少,一個簡單的if語句從鍵盤讀取,然後用另一個簡單的代碼行來改變主攝像機。只是谷歌「統一改變主攝像頭」 – Programmer