我在实例化功能方面有问题,如果我以不同的旋转拍摄,这个拍摄的形状会改变。在 x 轴(来自重生的条纹(中,它看起来不错(圆锥(,但例如在 y 轴中它看起来"平坦"。有什么想法可以解决吗? 在这里代码:
pellets[i] = Random.rotation;
p = (GameObject)Instantiate(pellet, barrelExit.position, barrelExit.rotation);
Destroy(p, lifeTime);
p.transform.rotation = Quaternion.RotateTowards(p.transform.rotation, pellets[i], spreadAngle);
p.GetComponent<Rigidbody>().AddForce(p.transform.right * pelletFireVelocity);
我不完全理解您通过固定角度而不是随机化来使用RotatateTowards
感觉它会或多或少地生成空锥体,但是可能不明白。我认为你应该用Random.Range(0.f, spreadAngle)
代替spreadAngle
另外,在旁注中,我认为您应该尝试对象池。
Random.rotation
在这里是不合适的。您对从每个可能的旋转中采样不感兴趣,只对围绕相机向前的旋转感兴趣。
相反,请使用Random.Range
获取介于-spreadAngle
和spreadAngle
之间的值。然后,您可以使用Quaternion.AngleAxis
将结果乘以枪管的旋转,围绕相机向前旋转那么多。
这也比使用RotateTowards
夹住喷雾更可取,因为这会导致更多的颗粒在喷雾的边缘发射。使用Random.Range
和Quaternion.AngleAxis
将导致均匀分布:
Camera mainCam; // cache result of Camera.main here for performance
Start()
{
...
mainCam = Camera.main;
...
}
...
float randomAngle = Random.Range(-spreadAngle, spreadAngle);
pellets[i] = Quaternion.AngleAxis(randomAngle, mainCam.forward) * barrelExit.rotation;
p = (GameObject)Instantiate(pellet, barrelExit.position, barrelExit.rotation);
Destroy(p, lifeTime);
p.transform.rotation = pellets[i];
p.GetComponent<Rigidbody>().AddForce(p.transform.right * pelletFireVelocity);
顺便说一下,您应该考虑汇集这些颗粒,而不是一次创建和销毁其中的许多颗粒。