Unity中实现相机平滑且流畅的跟随玩家(或其他对象)

https://kylewbanks.com/blog/unity-make-camera-follow-player-smoothly-and-fluidly


Unity: Make the Camera Follow a Player (or any GameObject) Smoothly and Fluidly

Unity中实现相机平滑且流畅的跟随玩家(或其他对象)


Having the Camera follow the player character is a common requirement for many types of games, so I thought I’d share a helpful trick to ensure the camera follows the player smoothly and fluidly.

相机跟随玩家是许多游戏的常见要求,所以只给分享一个有用的技巧,以保证相机平滑且流畅的跟随玩家。

The basic premise of having a camera follow the player is to update the camera’s position each frame to match that of the player’s. However, this can lead to unpleasant jerks in camera positioning when the player moves very quickly (teleportation, for instance), or if the game’s frame-rate dips.

相机跟随玩家的基本前提是每帧更新相机位置与玩家相匹配。然而,当玩家移动太快(比如:瞬移),或游戏帧率下降,这个会导致相机位置抖动,让人不爽。

The approach I prefer is to move the camera gradually towards the player’s position using Mathf.Lerp.

我比较喜欢使用Mathf.Learp,让相机平滑的朝向玩家位置移动。

What’s a Lerp?
什么是Learp?

The Lerp function takes two values, as well as an interpolation value between 0.0 and 1.0. As the documentation explains, when the interpolation value is 0.0, the first value is returned and when it’s 1.0 the second value is returned. However, if the interpolation value is somewhere between 0.0 and 1.0, an appropriate value between the two values is returned.

Lerp函数是两个值,值介于0.0与1.0值之间插值。所文档所述,当插值值为0.0时,第一个值为返回值,当它为1.0的时候返回第二个值。然而,若插值值在0.0和1.0之间的时候,则返回两个值之间的合适的值。

Consider the following examples:
如下面示例:


[C#]  纯文本查看  复制代码
?
 
1
2
3
4
Mathf.Lerp(10, 20, 0.5f); // Returns: 15.0f
 
Mathf.Lerp(10, 20, 0.25f); // Returns 12.5f
 
Mathf.Lerp(10, 20, 0.8f); // Returns 18.0f


If you’re curious, the formula looks like:
若你好奇,公式如下:
[AppleScript]  纯文本查看  复制代码
?
 
valueA + ( ( valueB - valueA ) * interpolationValue )


So, How do we Lerp?

怎么插值?

Lerp allows us to close the distance between two values, in our case the camera and player positions, gradually. It also means that the further the two points are from one another, the higher the value returned by Lerp will be, meaning we can move the camera more quickly the further away it is.

Lerp 取得相机位置和玩家位置这两个值之间的一个中间值。也就是说,离两个点中的另一个点越远,Lerp返回值越大,就意味着相机移动速度越快。

This gives us a really pleasant aesthetic with an elastic and fluid motion to close the gap between the two positions, and allows the player object to move slightly out of the center of the camera, with the camera moving to catch up with the player. Not only does present a much smoother motion, but it gives the player an additional sense of movement and control over their character.

这就给我们以减少两点之间差距,且具有美感和弹性的流程运动的感觉,还允许玩家偏离相机的中心位置,然后相机赶上玩家。不仅仅提供了更平滑的运动,而且给玩家一个额外的运动感和完全控制他们的角色。

 
Take a look at the sample below which demonstrates using Lerp on the y-axis:

看下下图的示例,在Y轴方向使用了Lerp.

While this example only demonstrates the y-axis, you could just as easily Lerp on the x-axis (or both simultaneously) as you’ll see below, depending on the type of game you’re making. You can also increase or decrease the speed at which the camera follows the player by adjusting the interpolation value - again, we’ll see this down below.

虽然这个例子只是演示了Y轴,很容易实现在X轴(或同时两个轴),在下面你会看到,这个取决于游戏类型。还可以通过调整插值来提高或降低相机的跟随速度——在下面的演示。


Let’s Code.

上代码

Alright so now that we know the general idea, let’s take a look at how this works in practice. We’ll create a standalone Follow script which, while intended for the camera and player in this case, can actually be applied to any GameObject to have it follow another.

好吧,现在知道了常规用法,我们来看看在粒子中怎么使用。创建一个独立的脚本,这种情况下适用于相机和玩家,但实际上可以用于任何GameObject的跟随。

[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
using UnityEngine;
using System.Collections;
 
public class Follow : MonoBehaviour {
 
     public GameObject objectToFollow;
 
     public float speed = 2.0f;
 
     void Update () {
         float interpolation = speed * Time.deltaTime;
 
         Vector3 position = this .transform.position;
         position.y = Mathf.Lerp( this .transform.position.y, objectToFollow.transform.position.y, interpolation);
         position.x = Mathf.Lerp( this .transform.position.x, objectToFollow.transform.position.x, interpolation);
 
         this .transform.position = position;
     }
}


How it works is by using the attached GameObject’s position (in our case it’s the Camera), and the position of the objectToFollow (the player), as well as a speed value. The value of speed* isn’t used directly tough, we instead multiply it by Time.deltaTime to create a smooth, frame-rate independent player following system.

怎么使用呢?通过关联GameObject对象的位置(本例中是相机),跟随对象(玩家)的位置,还有速度。速度并不是直接使用,而是乘以Time.deltaTime来实现根据跟帧率无关的平滑跟随系统。

Each frame, we update the current GameObject’s transform.position with the new x and y values returned by Mathf.Lerp.

每帧都通过Mathf.Learp来更新当前对象的位置的X和Y值。

With the Follow script written this way, we can add it to the Camera or any other GameObject, adjust the speed accordingly, and instantly create a smooth and fluid effect of following any GameObject in the Scene!

使用这样的Follow脚本,可以将它添加到Camera或其他任意的GameObject对象上,通过调整相应速度,在场景中创建一个流畅的跟随任意对象的效果。

原文地址:https://kylewbanks.com/blog/unity-make-camera-follow-player-smoothly-and-fluidly
作者:kylewbanks
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值