unity中相机追随

  • 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动

using UnityEngine;
using System.Collections; public class CameraFlow : MonoBehaviour
{
public Transform target;
private Vector3 offset; void Start()
{
//设置相对偏移
offset = target.position - this.transform.position;
} void Update()
{
this.transform.position = target.position - offset;
}
}
  • 固定相机跟随,带有角度旋转。这一种相机跟随是对第一种相机跟随的改进,在原有基础上面,添加了跟随角度的控制
using UnityEngine;
using System.Collections; public class CameriaTrack : MonoBehaviour
{
private Vector3 offset = new Vector3(0,5,4);//相机相对于玩家的位置
private Transform target;
private Vector3 pos;
public float speed = 2; // Use this for initialization
void Start ()
{
target = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame
void Update ()
{
pos = target.position + offset;
this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//调整相机与玩家之间的距离
Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//获取旋转角度
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime); }
}
  • 第三人称相机,这种相机跟随。是第三人称角度看向对象的,也就是一直看向对象的后面,如一直显示玩家的后背
using UnityEngine;
using System.Collections;
//相机一直拍摄主角的后背
public class CameraFlow : MonoBehaviour
{
public Transform target;
public float distanceUp=15f;
public float distanceAway = 10f;
public float smooth = 2f;//位置平滑移动值
public float camDepthSmooth = 5f;
// Use this for initialization
void Start ()
{ } // Update is called once per frame
void Update ()
{
// 鼠标轴控制相机的远近
if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
{
Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
}
} void LateUpdate()
{
//相机的位置
Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway; transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
//相机的角度
transform.LookAt(target.position);
}
}
  • 相机跟随,鼠标控制移动和缩放。相机与观察对象保持一定距离,可以通过鼠标进行上下左右旋转,通过鼠标滚轮进行放大和缩小操作

using UnityEngine;
using System.Collections; public class CameraFlow : MonoBehaviour
{
public Transform target;
Vector3 offset;
// Use this for initialization
void Start()
{
offset = transform.position - target.position;
} // Update is called once per frame
void Update()
{
transform.position = target.position + offset;
Rotate();
Scale();
}
//缩放
private void Scale()
{
float dis = offset.magnitude;
dis += Input.GetAxis("Mouse ScrollWheel") * 5;
Debug.Log("dis=" + dis);
if (dis < 10 || dis > 40)
{
return;
}
offset = offset.normalized * dis;
}
//左右上下移动
private void Rotate()
{
if (Input.GetMouseButton(1))
{
Vector3 pos = transform.position;
Vector3 rot = transform.eulerAngles; //围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
float x = transform.eulerAngles.x;
float y = transform.eulerAngles.y;
Debug.Log("x=" + x);
Debug.Log("y=" + y);
//控制移动范围
if (x < 20 || x > 45 || y < 0 || y > 40)
{
transform.position = pos;
transform.eulerAngles = rot;
}
// 更新相对差值
offset = transform.position - target.position;
}
}
}

最新文章

  1. 数据库日常维护-CheckList_03有关数据库数据文件大小检查
  2. [转]Android静态变量的生命周期
  3. Android studio .9图片造成的错误总结
  4. Oracle 常用函数
  5. c++builder调用VC的dll以及VC调用c++builder的dll
  6. iOS图像资源Images Assets
  7. TortoiseSVN文档
  8. AngularJs学习笔记-AngularJS权威教程学习笔记
  9. MAT(1) 小样
  10. UISlider的使用
  11. codevs2492 上帝造题的七分钟 2
  12. jenkins 设置 gitlab web hooks
  13. python3.5连接oracle数据及数据查询
  14. JGUI源码:实现简单MVVM单项绑定学习笔记(15)
  15. html_Dom
  16. JDK 和 OpenJDK 的区别
  17. python2和python3 安装pip冲突问题
  18. js实现农历时间代码
  19. luogu P4744 [Wind Festival]Iron Man
  20. Python3简单爬虫抓取网页图片

热门文章

  1. td中内容自动换行
  2. oracle12安装软件后安装数据库,然后需要自己配置监听
  3. js 考记忆力得小游戏
  4. linux 内核(系统)、函数的理解、宏的程序调试
  5. Oracle 使用TRUNCATE TABLE删除所有行
  6. SQL之相关语法及操作符
  7. 设计一个线程安全的单例(Singleton)模式
  8. css3动画学习资料整理
  9. Node.js下载及安装
  10. jQuery Validate(一)