Unity相机跟随-----根据速度设置偏移量
这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感。
但是由于惯性船的速度不一定在船的,因此可以获得当前船的速度方向在船的前方的投影分量,当分量与船的前方同向,那么设置偏移量为:速度分量的长度与船的最大比值t,乘以相机设定的最大偏移量
代码1
如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class CameraControl : MonoBehaviour { GameObject player;
public float speed=10f;
最大偏移量,这里最好能保证角色在屏幕范围内
public float radius;// Use this for initialization
void Start () { player = GameObject.FindWithTag("Player"); } // Update is called once per frame
void Update () {
Follow();
}void Follow()
{
if (!player)
{
player = GameObject.FindWithTag("Player");
}
else
{
Rigidbody2D rigid= player.GetComponent<Rigidbody2D>();
Ship ship = player.GetComponent<Ship>();
Vector3 playerVelocity = rigid.velocity;
//求出角色的速度在角色的正方向的分量速度
Vector3 playerVelocityOnForward= Vector3.Project(playerVelocity,player.transform.up);
//得到分量速度的方向与正方向是否同符号
float dot = Vector3.Dot(playerVelocityOnForward, player.transform.up);
//如果同符号,那么得到它的模与最大速度值的比值,用该比值乘以radius,即可得到相机的偏移位置量;如果不同号,即速度方向相反,那么比例值设置为0
float offsetLength = (dot >= ? playerVelocityOnForward.magnitude / ship.maxSpeed : ) * radius;
transform.position = Vector3.Lerp (transform.position, player.transform.position + player.transform.up* offsetLength - Vector3.forward, Time.deltaTime * speed);
}
} }
在代码1上可以看出,在飞船开动引擎和关闭引擎时,由于速度的剧烈变化,相机也会跟着剧烈变化,玩家眼睛无法跟上画面的剧烈变化,最好的解决办法是将radius的变化放缓,从而使得相机的动作流畅,便有了代码2.
代码2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class CameraControl : MonoBehaviour { GameObject player;
public float speed=10f;//相机的最大速度 public float radius; float minRadius=1f; float currentRadius; // Use this for initialization
void Start () {
player = GameObject.FindWithTag("Player");
} // Update is called once per frame
void Update () {
Follow();
}void Follow()
{
if (!player)
{
player = GameObject.FindWithTag("Player");
}
else
{
Rigidbody2D rigid= player.GetComponent<Rigidbody2D>();
Ship ship = player.GetComponent<Ship>();
Vector3 playerVelocity = rigid.velocity;
//求出角色的速度在角色的正方向的分量速度
Vector3 playerVelocityOnForward= Vector3.Project(playerVelocity,player.transform.up);
//得到分量速度的方向与正方向是否同符号
float dot = Vector3.Dot(playerVelocityOnForward, player.transform.up);
//如果同符号,那么得到它的模与最大速度值的比值,用该比值乘以radius,即可得到相机的偏移位置量;如果不同号,即速度方向相反,那么比例值设置为0
float offsetLength = (dot >= ? playerVelocityOnForward.magnitude / ship.maxSpeed : ) * radius;
//Debug.Log(offsetLength);
//如果按下了加速键,那么让相机在角色前方offsetLength处,否则,相机在角色前方最小偏移处
if (Input.GetKey(KeyCode.UpArrow))
{
if (currentRadius <= radius)
{
//currentRadius = currentRadius + Time.deltaTime * 3f;
//currentRadius = Mathf.Clamp(currentRadius, minRadius, radius);
//currentRadius = offsetLength;
currentRadius = Mathf.Lerp(currentRadius, offsetLength, Time.deltaTime * );
};
}
else
{
if (currentRadius > minRadius)
{
currentRadius = currentRadius - Time.deltaTime * 3f;
}
currentRadius = Mathf.Clamp(currentRadius, minRadius, radius);
}
transform.position = Vector3.Lerp(transform.position, player.transform.position + player.transform.up * currentRadius - Vector3.forward, Time.deltaTime * speed);
} } }
Unity相机跟随-----根据速度设置偏移量的更多相关文章
- unity相机跟随Player常用方式
固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...
- Unity相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- Unity中几种简单的相机跟随
#unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...
- Unity相机平滑跟随
简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...
- unity 常用的几种相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- unity3d简单的相机跟随及视野旋转缩放
1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...
- unity3D:游戏分解之角色移动和相机跟随
游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...
- Unity 相机
相机属性 1.相机的Clear属性:Skybo背景会渲染天空盒:solid color背景为颜色:depth only仅仅深度,相当于优先级:Don`t Clear背景是上一帧的图像:2.Projec ...
- HoloLens开发手记 - Unity之Recommended settings 推荐设置
Unity提供了大量的设置选项来满足全平台的配置,对于HoloLens,Unity可以通过切换一些特定的设置来启用HoloLens特定的行为. Holographic splash screen 闪屏 ...
随机推荐
- How-to Install VMware Tools on Debian Stretch 9 32/64bit Linux+GNU
在虚拟机VMWARE上安装debian9 安装vmwaretools时候遇到问题 询问我IFCONFIG安装在哪里? 新版的debian不知道是用户权限问题还是使用了其他网络配置工具 vmwareto ...
- vue入门:axios的应用及拦截封装
一.概述 在vue2.0项目中,我们主要使用axios进行http请求. axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特征: 1.从浏览器中创建X ...
- 在Tomcat中部署Spring jpetstore
第三篇:在Tomcat中部署Spring jpetstore 博客分类: Java之web SpringTomcatMySQLJDBCMVC Spring samples中的jpetstore,基于 ...
- R语言2版本3版本安装
./configure --prefix=/YZpath/public/software/R/R-3.5.0 --with-readline=no --with-x=no make make inst ...
- [C#]做服务使用Process启动外部程序没窗体
这几天会到一个需要,要时时侦测文件生成,并上传到Server上,侦测文件生成使用的FileSystemWatch.但是时时运行遇到了问题,程序可能会人为退出或者意外终止,使用一个进程监控程序的监程,也 ...
- word表格如何实现序号自动填充
打开word文档,我们需要在如下表格中的准考证号这一列中输入准考证号,手工输入肯定很慢,且容易出错. 我们先选中需要填充准考证号的表格. 选择功能区域中的“开始”,在“段落”组中点击“编号”按 ...
- 2018.11.01 NOIP训练 cost数(搜索+容斥原理)
传送门 唉考试的时候忘记剪倍数的枝了666666分滚粗. 其实就是一直取lcmlcmlcm搜索,然后容斥原理统计就行了. 代码
- WebSocket 长连接 及超时问题解决
<?phpset_time_limit(0); class SocketService { private $address = 'localhost'; private $port = 80; ...
- PARSEC安环境配置、运行
1.getting started 2.run PARSEC on simulators Full-System Simulators: such as Simics, GEM5.Trace-Driv ...
- 分分钟搞懂union与union all
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...