相机绑定角色思路+源码
发表于2015-07-30
![]() 将相机和角色绑定,让角色始终处于相机的中心,这样,无论角色如何移动,都不会跑出我们的视线了。 好,接下来讲一下相机绑定角色的基本原理,之后会附上源码。 1有一个可以绑架的角色, 2根据角色的移动,每一帧调整相机的位置。 3如果有鼠标右键旋转视角的功能,提供这个方法,并在每一帧的检查函数(也就是update或者lateupdate)中添加这个方法。 接下附上源码: void LateUpdate () { ScrollWithMouse (); RotateCameraByMouseRight (); } 这里每一帧检测两个方法,1是滚动鼠标调整相机和角色的距离 void ScrollWithMouse(){ fDistanceLerp = Mathf.Lerp(fDistanceLerp,fDistance,Time.deltaTime * 5); if (Input.GetAxis("Mouse ScrollWheel") != 0) { // get the distance between camera and target fDistance = Vector3.Distance (transform.position , player.transform.position); fDistance = Mathf.Clamp(fDistance - Input.GetAxis("Mouse ScrollWheel")*fScrollSpeed, fZoomMin, fZoomMax); } } 2是鼠标右键控制相机的旋转 void RotateCameraByMouseRight(){ if (Input.GetMouseButtonDown(1)) {bIsRotete = true;} if (Input.GetMouseButtonUp(1)) {bIsRotete = false;} if(bIsRotete){ x += Input.GetAxis("Mouse X") * fXspeed; y -= Input.GetAxis("Mouse Y") * fYspeed; y = Mathf.Clamp(y, fMinYLimit, fMaxYLimit); } ComputeDis(); } 注意ComputeDis()这个方法,这个方法是每一帧都会执行的,也是绑定角色的关键,方法内容如下 // change camera's rotation and position void ComputeDis(){ Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 calPos = new Vector3(0, 0, -fDistanceLerp); vPosition = rotation * calPos + player.transform.position; transform.rotation = rotation; transform.position = vPosition; }, 这里最终调整了相机的位置和旋转,使之始终以角色为屏幕的中心。附上全部代码: using UnityEngine; using System.Collections; public class cameracontroller : MonoBehaviour { public GameObject player; public float fXspeed,fYspeed,fMinYLimit,fMaxYLimit,fScrollSpeed,fZoomMin,fZoomMax; private float fDistance,fDistanceLerp,x,y; private Vector3 vPosition; private bool bIsRotete = false; // Use this for initialization void Start () { Vector3 angles = transform.eulerAngles; //at the world mouse move x camera move by y axis //mouse move y camera move by x axis x = angles.y; y = angles.x; fDistance = fZoomMax;fDistanceLerp = fDistance; ComputeDis (); } void LateUpdate () { ScrollWithMouse (); RotateCameraByMouseRight (); } void RotateCameraByMouseRight(){ if (Input.GetMouseButtonDown(1)) {bIsRotete = true;} if (Input.GetMouseButtonUp(1)) {bIsRotete = false;} if(bIsRotete){ x += Input.GetAxis("Mouse X") * fXspeed; y -= Input.GetAxis("Mouse Y") * fYspeed; y = Mathf.Clamp(y, fMinYLimit, fMaxYLimit); } ComputeDis(); } void ScrollWithMouse(){ fDistanceLerp = Mathf.Lerp(fDistanceLerp,fDistance,Time.deltaTime * 5); if (Input.GetAxis("Mouse ScrollWheel") != 0) { // get the distance between camera and target fDistance = Vector3.Distance (transform.position , player.transform.position); fDistance = Mathf.Clamp(fDistance - Input.GetAxis("Mouse ScrollWheel")*fScrollSpeed, fZoomMin, fZoomMax); } } // change camera's rotation and position void ComputeDis(){ Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 calPos = new Vector3(0, 0, -fDistanceLerp); vPosition = rotation * calPos + player.transform.position; transform.rotation = rotation; transform.position = vPosition; } } public GameObject player;//这个是要绑定的角色 public float fXspeed,fYspeed,fMinYLimit,fMaxYLimit,fScrollSpeed,fZoomMin,fZoomMax;//这是一些参数设置,xy方向旋转速度,y的最大值最小值,滚动速度,相机里角色的最大距离最小距离 我的参数设置如图: 使用方法:直接将脚本挂在相机身上,给图中参数设置初值,运行查看效果。 |