1.最普通的移动方式

1.transform.Translate(指定位置,指定参照坐标系(默认为Space.Self))
我认为Transform严谨来说并不能称作移动,而是直接改变物体的坐标。
 

2.刚体

1.MovePosition()

2.指定刚体的速度volicity
使物体忽略静摩擦力,从静止状态快速进入运动状态。

3.AddForce()
给物体添加一个方向力。
 

3.角色控制器

1.SimpleMove()
模拟重力,返回值表示当前角色是否着地。

2.Move()
不模拟重力,返回值表示角色于周围的碰撞信息。
 

4.平滑

1.Lerp()——线性插值(Vector2.Lerp,Vector3.Lerp,Mathf.Lerp,Quaternion.Lerp,Color.Lerp,Material.Lerp)

在Mathf中,Lerp函数是这样的Lerp(float a,float b,float t)。
a是 t是一个[0,1]的值,t的值代表a与b之间的哪个位置,t=0时返回a,t=1时返回b。

首先看一下Lerp是如何实现的:
Unity物体移动方法详解
用Clamp01函数去限制t的范围,之后再用计算公式:a+(b-a)*t计算到达的位置。
 

例如下面的例子:
当Time.time=0时,套入公式则2+(10-2)*0=2。
当Time.time=0.1时,套入公式则2+(10-2)*0.1=2.8。
当Time.time=0.2时,套入公式则2+(10-2)*0.2=2。
Unity物体移动方法详解
 

 

2.SmoothDamp()——平滑阻尼(Vector2.SmoothDamp,Vector3.SmoothDamp,Mathf.SmoothDamp)
由Damp可知是一个阻尼移动所以会受到阻力而减速,也就是越来越慢。

Vector3.SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed)
current是当前位置,target是目标位置,currentVelocity是当前速度(必须是一个全局变量),smoothTime是到达目标位置的时间,maxSpeed是最大速度(默认为无穷大)。

首先看一下SmoothDamp是如何实现的:
因为它不像Lerp那么简单它的内部参数经过了很多次的调整所以效果会更加平滑,常用于相机跟随操作。
Unity物体移动方法详解

它在表现上会相比Lerp好不少,
 

3.Slerp()——球形插值(Vector3.Slerp,Quaternion.Slerp)

5.特殊

1.Mathf.PingPong(float t,float length)——乒乓运动
返回一个[t,length]的值。t是一个变化的值。
length为0时会报NaN(Not a Number)的错误。

2.Math.MoveTowards(float from,float to,float maxDelta)
速度不会超过maxDelta。

3.Math.Repeat(float t,float length)
返回一个[t,length)的值,t是一个变化的值。

 

相关文章:

  • 2021-07-16
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案