Unity相机跟随
固定相机跟随
这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动
- using UnityEngine;
- using System.Collections;
- public class CameraFlow : MonoBehaviour
- {
- public Transform target;
- private Vector3 offset;
- // Use this for initialization
- void Start()
- {
- offset = target.position - this.transform.position;
- }
- // Update is called once per frame
- void Update()
- {
- this.transform.position = target.position - offset;
- }
- }
固定相机跟随,带有角度旋转
这一种相机跟随是对第一种相机跟随的改进,在原有基础上面,添加了跟随角度的控制
- using UnityEngine;
- using System.Collections;
- public class CameriaTrack : MonoBehaviour {
- private Vector3 offset = new Vector3(,,);//相机相对于玩家的位置
- private Transform target;
- private Vector3 pos;
- public float speed = ;
- // 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 < && Camera.main.fieldOfView >= ) || Input.mouseScrollDelta.y > && Camera.main.fieldOfView <= )
- {
- 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") * ;
- Debug.Log("dis=" + dis);
- if (dis < || dis > )
- {
- return;
- }
- offset = offset.normalized * dis;
- }
- //左右上下移动
- private void Rotate()
- {
- if (Input.GetMouseButton())
- {
- Vector3 pos = transform.position;
- Vector3 rot = transform.eulerAngles;
- //围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
- transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * );
- transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * );
- float x = transform.eulerAngles.x;
- float y = transform.eulerAngles.y;
- Debug.Log("x=" + x);
- Debug.Log("y=" + y);
- //控制移动范围
- if (x < || x > || y < || y > )
- {
- transform.position = pos;
- transform.eulerAngles = rot;
- }
- // 更新相对差值
- offset = transform.position - target.position;
- }
- }
- }
Unity相机跟随的更多相关文章
- unity相机跟随Player常用方式
固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...
- Unity相机跟随-----根据速度设置偏移量
这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感. ...
- Unity中几种简单的相机跟随
#unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...
- Unity相机平滑跟随
简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...
- unity 常用的几种相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- unity3D:游戏分解之角色移动和相机跟随
游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...
- unity3d简单的相机跟随及视野旋转缩放
1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...
- SurvivalShooter学习笔记(一.相机跟随)
1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...
- unity_实用小技巧(相机跟随两个主角移动)
在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置.方法如下: public Trans ...
随机推荐
- Centos7 修改/etc/profile错误后导致所有命令“not found”
因为Centos7中运行着两个版本的php,今天在设置环境变量时导致所有命令都 "not found". 修复方式: 第一:执行 /bin/vi /etc/profile 把文件修 ...
- JAVA并发同步互斥实现方式总结
大家都知道加锁是用来在并发情况防止同一个资源被多方抢占的有效手段,加锁其实就是同步互斥(或称独占)也行,即:同一时间不论有多少并发请求,只有一个能处理,其余要么排队等待,要么放弃执行.关于锁的实现网上 ...
- 工作问题--------爬虫遇到requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded
问题描述:爬取京东的网站,爬取一段时间后报错. 经过一番查询,发现该错误是因为如下: http的连接数超过最大限制,默认的情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再 ...
- 精简Command版SqlHelper
我在写CSharp程序对数据库进行操作时发现Connection对象起到了连接数据库的做用,实际执行SQL语句使用的是Command对象的方法,所以对SqlHelper进行了重写,具体如下: 一.创建 ...
- Python之三:运算符与表达式
1.运算符: 1.1.运算符种类: 运算符 名称 说明 例子 + 加 5+4 - 减 * 乘 / 除 // 取整除 商的整数部分 3//2,结果 ...
- Mysql快速入门(三)
MySQL性能优化之查看执行计划explain 介绍: (1).MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发 ...
- fastadmin选择下拉框
fastadmin中要做下拉框的效果如下: 数据库中数据: 在对应model中添加一个方法: 控制器中添加一行: 在目录lang/zh-cn中找到你控制器名称所对应的文件添加配置: 在add.html ...
- MySQL的字段属性+SQLyog查看建表语句
MySQL的字段属性 写在前面:数据库就是单纯的表,用来存储数据,只有行和列.行代表数据,列代表字段(id.name.age这种就叫字段) 1.长度 2.默认 3.主键 4.非空 5.Unsigned ...
- TD - 输入框
模板1:TD - 普通输入框 <input dojoType="bootstrap.form.ValidationTextBox" dojoAttachPoint=" ...
- <软件工程基础>
我是JX_Z,学习信息安全方向 //(怎么在这头不头尾不尾的地方弄个自我介绍这么尴尬呢) 之前也写过一些随笔记录自己的学习过程 软件工程基础课程中遇到的问题和学习心得都会记录在这篇文章中不断更新. 谢 ...