这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以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. mybatis 插入数据并返回主键值

    <insert id="insert" parameterType="com.pojo.TSubject" useGeneratedKeys=" ...

  2. 移动开发学习touchmove

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  3. Python : locals and globals

    Python有两个内置的函数,locals() 和globals(),它们提供了基于字典的访问局部和全局变量的方式.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个 字典,它的键字 ...

  4. 【转载】为什么任何随便输入的账号使用SYSDBA权限都能登陆oracle

    其实简单点就是检查一下你的机器有没有一个ora_dba用户组,而且你登陆os的用户是否在这个组里,有的话问题的原因就找到了,下面是转的高手的介绍 本文环境配置:Oracle10gR2,Windows ...

  5. hibernate项目

    http://blog.csdn.net/wzygis/article/details/22985751

  6. mysql之零碎知识

    一 视图 什么是视图:视图就是一张虚拟表.方便查看. 创建视图:create view 起名 as sql语句 #两张有关系的表 mysql> select * from course; +-- ...

  7. PHP中两个冒号是什么意思

    类中 静态方法和静态属性的引用方法 对类的静态属性和方法的直接引用,这种情况可以不需要实例化类而直接使用“::”调用

  8. my.ini优化mysql数据库性能的十个参数(推荐)

    (1).max_connections:允许的同时客户的数量.增加该值增加 mysqld 要求的文件描述符的数量.这个数字应该增加,否则,你将经常看到 too many connections 错误. ...

  9. tomcat7 安装 windows 服务

    tomcat 可以安装成windows 服务,这样 每次启动就不需要启动tomcat了. 具体配置: 1.修改 service.bat 在行首添加 set "JAVA_HOME=E:\jdk ...

  10. BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)

    4129: Haruna’s Breakfast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 835  Solved: 409[Submit][St ...