固定相机跟随

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

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFlow : MonoBehaviour
  5. {
  6. public Transform target;
  7. private Vector3 offset;
  8. // Use this for initialization
  9. void Start()
  10. {
  11. offset = target.position - this.transform.position;
  12.  
  13. }
  14.  
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. this.transform.position = target.position - offset;
  19. }
  20. }

固定相机跟随,带有角度旋转

这一种相机跟随是对第一种相机跟随的改进,在原有基础上面,添加了跟随角度的控制

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameriaTrack : MonoBehaviour {
  5. private Vector3 offset = new Vector3(,,);//相机相对于玩家的位置
  6. private Transform target;
  7. private Vector3 pos;
  8.  
  9. public float speed = ;
  10.  
  11. // Use this for initialization
  12. void Start () {
  13. target = GameObject.FindGameObjectWithTag("Player").transform;
  14.  
  15. }
  16.  
  17. // Update is called once per frame
  18. void Update () {
  19. pos = target.position + offset;
  20. this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//调整相机与玩家之间的距离
  21. Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//获取旋转角度
  22. this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);
  23.  
  24. }
  25. }

第三人称相机

这种相机跟随,是第三人称角度看向对象的,也就是一直看向对象的后面,如一直显示玩家的后背

  1. using UnityEngine;
  2. using System.Collections;
  3. //相机一直拍摄主角的后背
  4. public class CameraFlow : MonoBehaviour {
  5.  
  6. public Transform target;
  7.  
  8. public float distanceUp=15f;
  9. public float distanceAway = 10f;
  10. public float smooth = 2f;//位置平滑移动值
  11. public float camDepthSmooth = 5f;
  12. // Use this for initialization
  13. void Start () {
  14.  
  15. }
  16.  
  17. // Update is called once per frame
  18. void Update () {
  19. // 鼠标轴控制相机的远近
  20. if ((Input.mouseScrollDelta.y < && Camera.main.fieldOfView >= ) || Input.mouseScrollDelta.y > && Camera.main.fieldOfView <= )
  21. {
  22. Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
  23. }
  24.  
  25. }
  26.  
  27. void LateUpdate()
  28. {
  29. //相机的位置
  30. Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
  31. transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
  32. //相机的角度
  33. transform.LookAt(target.position);
  34. }
  35.  
  36. }

相机跟随,鼠标控制移动和缩放

相机与观察对象保持一定距离,可以通过鼠标进行上下左右旋转,通过鼠标滚轮进行放大和缩小操作

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFlow : MonoBehaviour
  5. {
  6. public Transform target;
  7. Vector3 offset;
  8. // Use this for initialization
  9. void Start()
  10. {
  11. offset = transform.position - target.position;
  12. }
  13.  
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. transform.position = target.position + offset;
  18. Rotate();
  19. Scale();
  20. }
  21. //缩放
  22. private void Scale()
  23. {
  24. float dis = offset.magnitude;
  25. dis += Input.GetAxis("Mouse ScrollWheel") * ;
  26. Debug.Log("dis=" + dis);
  27. if (dis < || dis > )
  28. {
  29. return;
  30. }
  31. offset = offset.normalized * dis;
  32. }
  33. //左右上下移动
  34. private void Rotate()
  35. {
  36. if (Input.GetMouseButton())
  37. {
  38. Vector3 pos = transform.position;
  39. Vector3 rot = transform.eulerAngles;
  40.  
  41. //围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
  42. transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * );
  43. transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * );
  44. float x = transform.eulerAngles.x;
  45. float y = transform.eulerAngles.y;
  46. Debug.Log("x=" + x);
  47. Debug.Log("y=" + y);
  48. //控制移动范围
  49. if (x < || x > || y < || y > )
  50. {
  51. transform.position = pos;
  52. transform.eulerAngles = rot;
  53. }
  54. // 更新相对差值
  55. offset = transform.position - target.position;
  56. }
  57.  
  58. }
  59. }

Unity相机跟随的更多相关文章

  1. unity相机跟随Player常用方式

    固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...

  2. Unity相机跟随-----根据速度设置偏移量

    这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感. ...

  3. Unity中几种简单的相机跟随

    #unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...

  4. Unity相机平滑跟随

    简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...

  5. unity 常用的几种相机跟随

    固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...

  6. unity3D:游戏分解之角色移动和相机跟随

          游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...

  7. unity3d简单的相机跟随及视野旋转缩放

    1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...

  8. SurvivalShooter学习笔记(一.相机跟随)

    1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...

  9. unity_实用小技巧(相机跟随两个主角移动)

    在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置.方法如下: public Trans ...

随机推荐

  1. Centos7 修改/etc/profile错误后导致所有命令“not found”

    因为Centos7中运行着两个版本的php,今天在设置环境变量时导致所有命令都 "not found". 修复方式: 第一:执行 /bin/vi /etc/profile 把文件修 ...

  2. JAVA并发同步互斥实现方式总结

    大家都知道加锁是用来在并发情况防止同一个资源被多方抢占的有效手段,加锁其实就是同步互斥(或称独占)也行,即:同一时间不论有多少并发请求,只有一个能处理,其余要么排队等待,要么放弃执行.关于锁的实现网上 ...

  3. 工作问题--------爬虫遇到requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded

    问题描述:爬取京东的网站,爬取一段时间后报错. 经过一番查询,发现该错误是因为如下: http的连接数超过最大限制,默认的情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再 ...

  4. 精简Command版SqlHelper

    我在写CSharp程序对数据库进行操作时发现Connection对象起到了连接数据库的做用,实际执行SQL语句使用的是Command对象的方法,所以对SqlHelper进行了重写,具体如下: 一.创建 ...

  5. Python之三:运算符与表达式

    1.运算符: 1.1.运算符种类: 运算符  名称  说明  例子  + 加    5+4  - 减      *  乘      /  除      //  取整除  商的整数部分  3//2,结果 ...

  6. Mysql快速入门(三)

    MySQL性能优化之查看执行计划explain 介绍: (1).MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发 ...

  7. fastadmin选择下拉框

    fastadmin中要做下拉框的效果如下: 数据库中数据: 在对应model中添加一个方法: 控制器中添加一行: 在目录lang/zh-cn中找到你控制器名称所对应的文件添加配置: 在add.html ...

  8. MySQL的字段属性+SQLyog查看建表语句

    MySQL的字段属性 写在前面:数据库就是单纯的表,用来存储数据,只有行和列.行代表数据,列代表字段(id.name.age这种就叫字段) 1.长度 2.默认 3.主键 4.非空 5.Unsigned ...

  9. TD - 输入框

    模板1:TD - 普通输入框 <input dojoType="bootstrap.form.ValidationTextBox" dojoAttachPoint=" ...

  10. <软件工程基础>

    我是JX_Z,学习信息安全方向 //(怎么在这头不头尾不尾的地方弄个自我介绍这么尴尬呢) 之前也写过一些随笔记录自己的学习过程 软件工程基础课程中遇到的问题和学习心得都会记录在这篇文章中不断更新. 谢 ...