这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以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. Mac虚拟机

    2018-06-21 需要的Mac静像是ios或是cdr的,如果是dmg,可以参考这个转换http://blog.sina.com.cn/s/blog_60b45f230101kkbf.html 或  ...

  2. 用php脚本比较MySQL两个数据库的结构差异

    define('DATABASE1', 'mysql://root:password@127.0.0.1/db1'); $dbi1 = new DbMysql; $dbi1->dbh = DAT ...

  3. js之function

    function* function* 这种声明方式(function关键字后跟一个星号)会定义一个生成器函数 (generator function),它返回一个  Generator  对象. 你 ...

  4. kbmmw 的远程桌面功能2-android手机端

    前面讲了PC 端的远程操作,今天讲一下如何用手机控制远程桌面(真的能操作的很爽吗?), 要使手机可以远程控制,首先在在kbmmwconfig.inc 文件里面加入FMX 支持 {$DEFINE KBM ...

  5. Le Chapitre VIII

    J'appris bien vite à mieux connaître cette fleur. Il y avait toujours eu, sur la planète du petit pr ...

  6. (14)Why some people find exercise harder than others

    https://www.ted.com/talks/emily_balcetis_why_some_people_find_exercise_harder_than_others/transcript ...

  7. adb 相关命令 以及无法adb识别设备的解决方法

    [自己解决方法] 在-/.android/文件夹下面新建adb_usb.ini文件.里面写入设备的idVendor号(0x加上四位数字),然后输入 adb kill-server, 然后adb dev ...

  8. Linux主机名域名修改问题

    1.修改 /etc/sysconfig/network  配置文件 vi  /etc/sysconfig/network 修改HOSTNAMEHOSTNAME=new-hostname.domainn ...

  9. vmware虚拟机centOs安装教程

    1安装vmware 虚拟机软件 1.解压vmware安装 汉化vmware虚拟机 复制注册码,并填写进vmware 2安装linux(centos)虚拟机 1.  点击文件----->新建虚拟机 ...

  10. 20171126--idleHadnler的理解使用

    相关参考文献: http://www.jianshu.com/p/94d6131a53b2 http://bbs.51cto.com/thread-1094228-1.html https://zhu ...