Unity3D实现相机跟随控制的方法

发布时间:2020-07-07 11:29:47 作者:清晨
来源:亿速云 阅读:787

这篇文章将为大家详细讲解有关Unity3D实现相机跟随控制的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

跟随算法

要实现3D摄像机的控制第一步就是先实现摄像机跟随物体移动。
要想让相机跟随物体移动,就要明白在一定角度下相机与物体的位置关系。

首先设置相机与物体之间的距离distance,相机与xz平面的角度为roll
所以根据三角关系可以求得映射在xz平面的距离d为distancecos(rool),相机高度为distancesin(roll)。
如下图

Unity3D实现相机跟随控制的方法

现在就可以确定相机的高度了即y轴的坐标相机的y轴坐标应该为 Camera.Main.y=物体.y+height

在xz平面中,设相机与物体的距离为d(就是上面说的那个d,distance映射在xz平面的长度),相机的旋转角度为rot。根据下图可以看到,相机与物体的连线与x轴的角度为rot-180.根据三角函数,既可以得出x轴的位移为d*sin(rot) ,z轴的位移为d*cos(rot)

Unity3D实现相机跟随控制的方法

所以说开始的时候指定distance和rot和roll就可以实现跟随了。实现跟随的代码如下

public class CameraFollow : MonoBehaviour
{
 //距离
 public float distance = 15;
 //横向角度
 public float rot = 0;
 //纵向角度 30d度
 public float roll = 30f * Mathf.PI * 2 / 360;
 //目标物体
 public GameObject target;
 
 private void Start()
 {
 target = GameObject.Find("Black Track");
 }
 
 private void LateUpdate()
 {
 if (target == null)
  return;
 if (Camera.main == null)
  return;
  
 //目标的坐标
 Vector3 targetPos = target.transform.position;
 //用三角函数计算相机的位置
 Vector3 cameraPos;
 float d = distance * Mathf.Cos(roll);
 float height = distance * Mathf.Sin(roll);
 cameraPos.x = targetPos.x + d * Mathf.Cos(rot);
 cameraPos.z = targetPos.z + d * Mathf.Sin(rot);
 cameraPos.y = targetPos.y + height;
 Camera.main.transform.position = cameraPos;
 Camera.main.transform.LookAt(target.transform);
 }
}

在跟随的时候我们可以在要跟随的物体下放置一个子物体命名为cameraPoint使相机对准这个子物体从而方便的更改摄像机的视角。
所以在物体下添加一个cameraPoint的子物体

Unity3D实现相机跟随控制的方法

并且添加代码

//设置目标
 public void SetTarget(GameObject target)
 {
 if (target.transform.Find("cameraPoint") != null)
  this.target = target.transform.Find("cameraPoint").gameObject;
 else
  this.target = target;
 }

如果准的物体有名为cameraPoint的子物体,那么相机对准cameraPoint子物体。

横向与纵向旋转摄像机

当鼠标向左移动时,相机随之左转,当鼠标向右移动时,相机随之右转。
Unity的输入轴Mouse X 和 Mouse Y 代表着鼠标的移动增量,也就是说当鼠标向左移动时,Input.GetAxis(“Mouse X”)的值会增大,向右则减少。只要让旋转角度rot与Mouse X成正比关系,便能通过鼠标控制摄像机的角度。
代码如下

//横向旋转速度
public float rotSpeed=0.1f;
//横向旋转
public void Rotate()
 {
 float w = Input.GetAxis("Mouse X") * rotSpeed;
 rot -= w;
 }

同理对于纵向旋转我们需要设定一个范围 所以代码如下

//纵向旋转角度
public float maxRoll = 70f * Mathf.PI * 2 / 360;
public float minRoll = 0f * Mathf.PI * 2 / 360;
//纵向旋转速度
private float rollSpeed = 0.1f;
//纵向旋转
public void Roll()
 {
 float w = Input.GetAxis("Mouse Y") * rollSpeed;
 roll -= w;
 if (roll > maxRoll)
  roll = maxRoll;
 if (roll < minRoll)
  roll = minRoll;
 }

滚轮调节距离

通过鼠标滚轮调整相机与物体之间的距离
代码如下

//距离范围
public float maxDistance = 22f;
public float minDistance = 5f;
//距离变化速度
public float zoomSpeed = 0.2f;
//调整距离
public void Zoom()
 {
 if(Input.GetAxis("Mouse ScrollWheel") >0)
 {
  if (distance > minDistance)
  distance -= zoomSpeed;
 }
 else if (Input.GetAxis("Mouse ScrollWheel") < 0)
 {
  if (distance < maxDistance)
  distance += zoomSpeed;
 }
 }

全部代码

public class CameraFollow : MonoBehaviour
{
 //距离
 public float distance = 15;
 //横向角度
 public float rot = 0;
 //纵向角度 30d度
 public float roll = 30f * Mathf.PI * 2 / 360;
 
 //目标物体
 public GameObject target;
 
 //横向旋转速度
 public float rotSpeed=0.1f;
 
 //纵向旋转角度
 public float maxRoll = 70f * Mathf.PI * 2 / 360;
 public float minRoll = 0f * Mathf.PI * 2 / 360;
 //纵向旋转速度
 private float rollSpeed = 0.1f;
 
 //距离范围
 public float maxDistance = 22f;
 public float minDistance = 5f;
 //距离变化速度
 public float zoomSpeed = 0.2f;
 
 private void Start()
 {
 target = GameObject.Find("Black Track");
 SetTarget(target);
 }
 
 private void LateUpdate()
 {
 if (target == null)
  return;
 if (Camera.main == null)
  return;
 //横向旋转
 Rotate();
 //纵向旋转
 Roll();
 //缩放
 Zoom();
 //目标的坐标
 Vector3 targetPos = target.transform.position;
 //用三角函数计算相机的位置
 Vector3 cameraPos;
 float d = distance * Mathf.Cos(roll);
 float height = distance * Mathf.Sin(roll);
 cameraPos.x = targetPos.x + d * Mathf.Cos(rot);
 cameraPos.z = targetPos.z + d * Mathf.Sin(rot);
 cameraPos.y = targetPos.y + height;
 Camera.main.transform.position = cameraPos;
 Camera.main.transform.LookAt(target.transform);
 }
 
 //设置目标
 public void SetTarget(GameObject target)
 {
 if (target.transform.Find("cameraPoint") != null)
  this.target = target.transform.Find("cameraPoint").gameObject;
 else
  this.target = target;
 }
 
 //横向旋转
 public void Rotate()
 {
 float w = Input.GetAxis("Mouse X") * rotSpeed;
 rot -= w;
 }
 
 //纵向旋转
 public void Roll()
 {
 float w = Input.GetAxis("Mouse Y") * rollSpeed;
 roll -= w;
 if (roll > maxRoll)
  roll = maxRoll;
 if (roll < minRoll)
  roll = minRoll;
 }
 
 //调整距离
 public void Zoom()
 {
 if(Input.GetAxis("Mouse ScrollWheel") >0)
 {
  if (distance > minDistance)
  distance -= zoomSpeed;
 }
 else if (Input.GetAxis("Mouse ScrollWheel") < 0)
 {
  if (distance < maxDistance)
  distance += zoomSpeed;
 }
 }
}

关于Unity3D实现相机跟随控制的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Unity3d学习 相机的跟随
  2. [unity3d]如何实现游戏对象跟随鼠标方向移动

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity3d 相机跟随 un

上一篇:使用linux的setquota命令设置文件系统配额

下一篇:解决Android AVD启动报错问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》