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

    CXF 编辑     目录 1Apache CXF 简介 关于Apache CXF 功能特性 项目目标 2Apache CXF特点 灵活部署 支持多种编程语言 代码生成     1Apache CXF ...

  2. 如何在Android平台上使用USB Audio设备

    http://blog.csdn.net/kevinx_xu/article/details/12951131 需求:USB Headset插上去后,声音要从本地CODEC切换到USB Headset ...

  3. ssh安装

    http://blog.chinaunix.net/uid-20791108-id-3761681.htmlhttp://www.cnblogs.com/mliudong/p/4094519.html ...

  4. layer.alert没有垂直居中

    经查找是因为 <!DOCTYPE html> 这句没有写在整个页面的最顶部,将其放在整个页面的第一行就可以了. ps:原理不是很清楚

  5. Java基础-时间类

    关于java中六个时间类的使用和区别 java.util.Date java.sql.Date ,java.sql.Time , java.sql.Timestamp java.text.Simple ...

  6. C++ cout

    cout.flush() cout.put() 输出一个字符 char* p = "hello"; cout.write(p,sttrlen(q)-3) 输出字符串,能够选定长度. ...

  7. verilog中的多维数组

    reg  arrayb [7:0] [0:255] ;//二维数组.

  8. redis概览

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括string(字符串 ...

  9. ADALINE小demo

    线性逼近 clear;clc;close all x = [1,0.5; 1.5,1.1; 3,3; -1.2,-1]; y = x(:,2); x = [ones(size(x,1),1),x(:, ...

  10. Using Spring.net in console application

    Download Spring.net in http://www.springframework.net/ Install Spring.NET.exe Create a console appli ...