unity3d游戏开发之相机切换.docx
unity3d游戏开发之相机切换相机切换 为了简单演示,使用一个立方体和一个球体作为两个人物 1、首先为立方体和球体分别添加脚本、刚体(去掉使用重力)和角色控制器(CharacterController),使它们可以前后左右移动 public class CubeCamera : MonoBehaviour private CharacterController cube; void Start cube = GetComponent<CharacterController> void Update float x = Input.GetAxis("Horizontal") * speed; float z = Input.GetAxis("Vertical") * speed; cube.SimpleMove(new Vector3(x, 0, z); 2、为立方体和球体添加子物体:摄像机,先使立方体的摄像机不可用,使球体的摄像机可用 3、勾选上立方体和球体上面的IsTrigger属性,修改它们的脚本,完成球体碰撞到立方体时,球体摄像机 球体脚本: public class SphereCamera : MonoBehaviour private CharacterController sphere; float speed = 5f; public GameObject sphereCamera; public GameObject cubeCamera; bool flage = true; void Start sphere = GetComponent<CharacterController> / Update is called once per frame void Update if(flage) float x = Input.GetAxis("Horizontal") * speed; float z = Input.GetAxis("Vertical") * speed; sphere.SimpleMove(new Vector3(x, 0, z); void OnTriggerEnter(Collider other) if(other.tag="Cube") flage = false; sphereCamera.SetActive(false);/球体摄像机不可用 cubeCamera.SetActive(true);/立方体摄像机可用 CubeCamera.flag = true; 立方体脚本: public class CubeCamera : MonoBehaviour private CharacterController cube; float speed = 5f; public static bool flag = false; void Start cube = GetComponent<CharacterController> / Update is called once per frame void Update if (flag) float x = Input.GetAxis("Horizontal") * speed; float z = Input.GetAxis("Vertical") * speed; cube.SimpleMove(new Vector3(x, 0, z); 这篇文章来自狗刨学习网