为了实现上图比较平滑的追踪,又要保证追踪子弹能命中目标,
增加一个变量记录追踪的力度,并且力度随时间成长,
用追踪力度决定子弹原本方向的改变大小,
特殊的,为了防止子弹在目标点附近打圈,在距离目标较近时,直接设置方向
演示代码
x
20
1
private void Update()
2
{
3
if (IsArrive())
4
{
5
Destroy(gameObject);
6
return;
7
}
8
9
Vector3 tarGetDir = targetPos - transform.position;
10
float dis = tarGetDir.magnitude;
11
tarGetDir = tarGetDir / dis;
12
13
if (dis <= 1)
14
transform.forward = tarGetDir;
15
else
16
transform.forward = Vector3.Slerp(transform.forward, tarGetDir, homingWeight * Time.deltaTime);
17
18
homingWeight += Time.deltaTime * 8;
19
transform.position += transform.forward * Speed * Time.deltaTime;
20
}