【发布时间】:2014-09-29 06:59:04
【问题描述】:
我正在尝试设置一个对象以在发射时以 4 种方式实例化子弹。例如如下:
Assuming 'A' is the object:
^
|
<- A ->
|
v
我正在尝试将其设置为按上述指定方向拍摄 4 种方式。当前设置有效,并且预制件在方向上正确实例化。问题不是所有的射门都以相同的速度射出。右侧和底部的射门正确,而顶部和左侧的射门速度较慢。
所有方向的代码设置都完全相同,并且所有方向都使用相同的预制件和相同的速度,因此对速度差异的原因感到困惑。请指教。谢谢。
public Rigidbody bulletPrefab;
//empty gameobjects to locate the positions
public Transform bulletStartPosRight, bulletStartPosLeft, bulletStartPosTop, bulletStartPosBottom;
private float bulletSpeed = 300;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody bulletInstance1, bulletInstance2, bulletInstance3, bulletInstance4;
bulletInstance1 = Instantiate(bulletPrefab, bulletStartPosRight.position, bulletStartPosRight.rotation) as Rigidbody;
//the force is always forward. I rotated the empty gameobjects in Unity instead of changing the code here to forward,up,right and so on.
bulletInstance1.AddForce(bulletStartPosRight.forward * bulletSpeed);
bulletInstance2 = Instantiate(bulletPrefab, bulletStartPosLeft.position, bulletStartPosLeft.rotation) as Rigidbody;
bulletInstance2.AddForce(bulletStartPosLeft.forward * bulletSpeed);
bulletInstance3 = Instantiate(bulletPrefab, bulletStartPosTop.position, bulletStartPosTop.rotation) as Rigidbody;
bulletInstance3.AddForce(bulletStartPosTop.forward * bulletSpeed);
bulletInstance4 = Instantiate(bulletPrefab, bulletStartPosBottom.position, bulletStartPosBottom.rotation) as Rigidbody;
bulletInstance4.AddForce(bulletStartPosBottom.forward * bulletSpeed);
}
}
【问题讨论】:
-
您可以尝试直接分配新刚体的
velocity属性,而不是调用AddForce。这避免了预先存在的速度可能影响子弹的任何情况。除此之外,您应该仔细检查所有轮换是否完全符合您的预期。 -
旋转是正确的,速度仍然给出相同的结果。提取了这部分问题并创建了一个小的 Unity 项目文件。如果你有时间,一定要看看。保管箱链接在上面的问题中。谢谢。