unity3d游戏开发之相机切换.doc
文本预览下载声明
相机切换
为了简单演示,使用一个立方体和一个球体作为两个人物
1、首先为立方体和球体分别添加脚本、刚体(去掉使用重力)和角色控制器(CharacterController),使它们可以前后左右移动
public class CubeCamera : MonoBehaviour {
? private CharacterController cube;
void Start () {
? ? ? ? ?cube = GetComponentCharacterController();
}
void Update () {
? ? ? ? ? ? ?float x = Input.GetAxis(Horizontal) * speed;
? ? ? ? ? ?? float z = Input.GetAxis(Vertical) * speed;
? ? ? ? ? ? ?cube.SimpleMove(new Vector3(x, 0, z));
? ? ? ? }
}
2、为立方体和球体添加子物体:摄像机,先使立方体的摄像机不可用,使球体的摄像机可用【 HYPERLINK \\+target=\\_blank\\+target=\\_blank/ \t /_blank 狗刨学习网】
3、勾选上立方体和球体上面的IsTrigger属性,修改它们的脚本,完成球体碰撞到立方体时,球体摄像机
? ? ? 球体脚本:
public class SphereCamera : MonoBehaviour {
? ? private CharacterController sphere;
? ? float speed = 5f;
? ? public GameObject sphereCamera;
? ? public GameObject cubeCamera;
? ? bool flage = true;
? ? void Start()
? ? {
? ? ? ? sphere = GetComponentCharacterController();
? ? }
? ? // 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 = GetComponentCharacterController();
}
// 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));
? ? ? ? }
? ? ? ?
}
}这篇文章来自 HYPERLINK / 狗刨学习网
显示全部