这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以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相机跟随-----根据速度设置偏移量的更多相关文章

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

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

  2. Unity相机跟随

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

  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简单的相机跟随及视野旋转缩放

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

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

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

  8. Unity 相机

    相机属性 1.相机的Clear属性:Skybo背景会渲染天空盒:solid color背景为颜色:depth only仅仅深度,相当于优先级:Don`t Clear背景是上一帧的图像:2.Projec ...

  9. HoloLens开发手记 - Unity之Recommended settings 推荐设置

    Unity提供了大量的设置选项来满足全平台的配置,对于HoloLens,Unity可以通过切换一些特定的设置来启用HoloLens特定的行为. Holographic splash screen 闪屏 ...

随机推荐

  1. maven下载的jar相应pom文件下载不完整问题。

    今天遇到一个奇葩问题: 同样的项目,我启动报错 : 某个class文件找不到.. 查找maven 依赖也的确没有找到 对应的jar 包. 查找同事项目,可以看到该class对应的 jar 包 是 lo ...

  2. mybatis学习 十二 多表查询

    Mybatis 实现多表查询方式: (1)业务装配.对两个表编写单表查询语句,在业务(Service)把查询的两个结果进行关联. (2)使用Auto Mapping特性,在实现两表联合查询时通过别名完 ...

  3. django之content_type

    什么是content type:django内置的一个组件,这个组件帮忙做连表的操作.(混搭连表) 适用场景:适用于一张表与多张表同时做关联的时候.直接导入就可以使用了. 关联数据库说有的表:让我们可 ...

  4. static与非static的区别

    static 静态的,可以修饰变量或者方法 用于变量的区别 1. static 修饰的变量称为类变量或全局变量或成员变量,在类被加载的时候成员变量即被初始化,与类关联,只要类存在,static变量就存 ...

  5. 2019.01.13 bzoj1146: [CTSC2008]网络管理Network(整体二分+树剖)

    传送门 题意简述:给一棵树,支持单点修改,询问路径上两点间第kkk大值. 思路: 读懂题之后立马可以想到序列上带修区间kkk大数的整体二分做法,就是用一个bitbitbit来支持查值. 那么这个题把树 ...

  6. 关于redis,学会这9点就够了

    一.redis是什么 redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串.哈希.列表.队 ...

  7. pipenv知识积累

    pip install pipenv 安装pipenv pipenv --python 3.6 指定某一Python版本创建环境 pipenv --py 显示Python解释器信息 pipenv -- ...

  8. Interrouter Signals

    summary of traditional NoC interrouter signals summary of SMART interrouter signals flit_valid and f ...

  9. Jquery 的ajax里边不能识别$(this)

    确实不能用,在ajax外面弄个变量$this= $(this),然后在里面用就行了 在jQuery使用ajax后$(this)失效,原因很简单,$(this)指向的是最近调用它的jquery对象,即$ ...

  10. auto和decltype(c++11)

    1.auto 1)auto是一个类型说明符(类型说明符就是像int.double这样的),用来定义一个变量,它可以让编译器去分析表达式的类型,并使用该表达式的值去初始化变量 //auto定义的变量必须 ...