Unity3D游戏开发之第三人称摄像机的简单实现(跟随视角,自由视角)
发表于2017-06-07
前面已经跟大家介绍了Unity的一些基础知识,接下来我们将要对一些基本功能做一些学习。大家都知道,一个游戏,少不了摄像机的参与。摄像机,总体来说分为了三大类别:上帝视角,也就是俗称的第三人称视角,以一种凌驾于游戏object的方式存在,玩家跟上帝一般俯瞰整个游戏;主角视角,第一人称视角,以主角的角度观看游戏的发展状态;观察视角,关注于某个物体的视角,这个视角在一些密室逃脱类游戏里经常有。
下面我们来学习一下简单的第三人称视角
一、第三人称跟随视角
这个视角是摄像机始终处于被观察物体(主角)的某个角度,跟随着主角的移动来调整自己的位置并始终观察着主角。
现在我们点开Window->AssetStore,搜索UnityChan,这是一个精美并且免费的模型,我们可以用它来作为demo,下载并导入场景
接下来我们就要编写摄像机的脚本了,这个脚本比较简单
123456789101112131415161718192021222324252627 using UnityEngine;using System.Collections; public class CameraFollow : MonoBehaviour { public Transform target; public float distanceH = 7f; public float distanceV = 4f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void LateUpdate() { Vector3 nextpos = Vector3.forward * -1 * distanceH + Vector3.up * distanceV + target.position; this.transform.position = nextpos; this.transform.LookAt(target); }}
只要把上面的脚本付给摄像头,然后在Inspector界面把主角拖进Target里面,运行,移动主角的时候我们就能发现摄像头也在跟着主角移动了。
细心的朋友可能也发现了,就是如果转动主角的时候,摄像机还是处于一个角度观察主角,如果说我希望摄像头始终在主角的背面,那怎么办呢。我们只需要做一些小小的改动就可以了
12345678 void LateUpdate() { Vector3 nextpos = target.forward * -1 * distanceH + target.up * distanceV + target.position; this.transform.position = nextpos; this.transform.LookAt(target); }
这样,大家试试转动主角,可以发现,摄像机始终都是看到主角的背面了~一个非常简单实用的跟随摄像机就出来了~
PS:下面我们说一些小技巧
- 摄像机的更新最好在LateUpdate()里面执行,因为这个时候游戏的所有行为基本上已经处理完,在这个时候来进行摄像机的更新是最好的时候,能够把游戏最新的状态呈现出来
- 如果想要摄像机缓慢跟随,可以加上平滑,具体代码如下:
12345678910 public float smoothSpeed = 10f; //平滑参数 void LateUpdate() { Vector3 nextpos = target.forward * -1 * distanceH + target.up * distanceV + target.position; this.transform.position =Vector3.Lerp(this.transform.position, nextpos, smoothSpeed * Time.deltaTime); //平滑插值 this.transform.LookAt(target); }
二,第三人称自由视角
这个模式,说白了就是将视角当成一个object来进行移动,就跟你在Scene界面里面的操作一样
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 using UnityEngine;using System.Collections; public class CameraMover : MonoBehaviour{ public float cameraMoveSpeed = 10f; public float cameraRotSpeed = 30f; bool isRotateCamera = false; private float trans_y = 0; private float trans_x = 0; private float trans_z = 0; private float eulerAngles_x; private float eulerAngles_y; // Use this for initialization void Start() { Vector3 eulerAngles = this.transform.eulerAngles;//当前物体的欧拉角 this.eulerAngles_x = eulerAngles.y; this.eulerAngles_y = eulerAngles.x; } void FixedUpdate() { if (Input.GetMouseButton(1)) { this.eulerAngles_x += (Input.GetAxis("Mouse X") * this.cameraRotSpeed) * Time.deltaTime; this.eulerAngles_y -= (Input.GetAxis("Mouse Y") * this.cameraRotSpeed) * Time.deltaTime; Quaternion quaternion = Quaternion.Euler(this.eulerAngles_y, this.eulerAngles_x, (float)0); this.transform.rotation = quaternion; moveCameraByKey(cameraMoveSpeed); } this.trans_z = (Input.GetAxis("Mouse ScrollWheel") * this.cameraMoveSpeed * 2) * Time.deltaTime; this.transform.Translate(Vector3.forward * this.trans_z); //if (Input.GetMouseButton(2)) //{ // this.trans_y = (Input.GetAxis("Mouse Y") * this.ySpeed / 2) * 0.02f; // this.trans_x = (Input.GetAxis("Mouse X") * this.xSpeed / 2) * 0.02f; // this.transform.Translate(-1 *Vector3.right * this.trans_x); // this.transform.Translate(-1 *Vector3.up * this.trans_y); //} } void moveCameraByKey(float speed) { if (Input.GetKey(KeyCode.Q)) { this.transform.Translate(Vector3.down * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.E)) { this.transform.Translate(Vector3.up * speed *Time.deltaTime); } float moveV = Input.GetAxis("Vertical"); float moveH = Input.GetAxis("Horizontal"); this.transform.Translate(Vector3.forward * speed * moveV * Time.deltaTime + Vector3.right * speed * moveH * Time.deltaTime); } }
这一篇就先到这了,下一篇我们会学习一下观察视角~希望大家能支持吧(●’◡’●)
Unity3D游戏开发系列:
