摄像机比作人的眼睛最适合不过了。比如:极品飞车里面可变换视觉角度,可以从多个角度观察汽车。
切换摄像机:一个键控制一个摄像机
这里是有多少台摄像机就要设置多少个建,有些场景可能有十多个视角,比如NBA2K游戏里面就有多达12个视角以上,下面这种方法就不适用了,
- using UnityEngine;
- using System.Collections;
- public class ActiveCamera : MonoBehaviour {
- public GameObject camFirst;
- public GameObject camThree;
- public GameObject camSky;
- public float changeInterval = 1.0f;
- public string showStr = "";
- void Start () {
- FindCamera();
- }
- //找到摄像机对象
- void FindCamera()
- {
- camFirst = GameObject.Find("Main Camera");//第一人称视角
- camThree = GameObject.Find("CameraThree");//第三人称视角
- camSky = GameObject.Find("CameraSky");//空中俯瞰视角
- camFirst.active = true;
- camThree.active = false;
- camSky.active = false;
- }
- void Update () {
- }
- void OnGUI()
- {
- ChangeAngle1();
- }
- void ChangeAngle1()
- {
- if (Input.GetKey(KeyCode.Alpha1))
- {
- SetFalse();
- camFirst.active = true;
- showStr = "切换至第三人称视角";
- }
- else if (Input.GetKey(KeyCode.Alpha2))
- {
- SetFalse();
- camSky.active = true;
- showStr = "切换至空中视角";
- }
- else if (Input.GetKey(KeyCode.Alpha3))
- {
- SetFalse();
- camThree.active = true;
- showStr = "切换至第一人称视角";
- }
- Debug.Log(showStr);
- // GUILayout.Button(showStr);
- }
- void SetFalse()
- {
- camFirst.active = false;
- camThree.active = false;
- camSky.active = false;
- }
- }
切换摄像机:一个键控制N个摄像机
下面尝试的代码不起效果,有待解决
- void ChangeAngle2()
- {
- if (Input.GetKey(KeyCode.Z))
- {
- if (camFirst.active)
- {
- camFirst.active = false;
- camThree.active = false;
- camSky.active = true;
- showStr = "切换至第三人称视角";
- }
- if (camThree.active)
- {
- camFirst.active = false;
- camThree.active = false;
- camSky.active = true;
- showStr = "切换至空中视角";
- }
- if (camSky.active)
- {
- camFirst.active = true;
- camThree.active = false;
- camSky.active = false;
- showStr = "切换至第一人称视角";
- }
- }
- Debug.Log(showStr);
- //GUILayout.Button(showStr);
- }
关于Unity3D,我们有个专门技术讨论的大群,可以进行技术交流和咨询,群号:858550 欢迎进行技术讨论,里面有不少大牛