在这段代码中摧毁射弹的尝试不起作用。任何建议都表示赞赏。
Rigidbody InstantiateedProjectileLeft = Instantiate(cannonAmmo, firingPointLeft.transform.position, firingPointLeft.transform.rotation) as Rigidbody;
if (InstantiateedProjectileLeft != null)
{
//print ("Firing projectile");
InstantiateedProjectileLeft.transform.Translate(Vector3.forward);
InstantiateedProjectileLeft.rigidbody.AddForce(transform.forward * cannonAmmoSpeed);
//print (InstantiateedProjectileLeft.transform.position.y);
print ("Destroying left projectile");
//InstantiateedProjectileLeft.renderer.material.color = Color.clear;
Destroy(InstantiateedProjectileLeft, 1.0f); // Doesn't work
}
Rigidbody InstantiateedProjectileRight = Instantiate(cannonAmmo, firingPointRight.transform.position, firingPointRight.transform.rotation) as Rigidbody;
if (InstantiateedProjectileRight != null)
{
//print ("Firing projectile");
InstantiateedProjectileRight.transform.Translate(Vector3.forward);
InstantiateedProjectileRight.rigidbody.AddForce(transform.forward * cannonAmmoSpeed);
Destroy(InstantiateedProjectileRight, 1.0f); // Doesn't work
}
答案 0 :(得分:2)
你需要在GameObject上调用Destroy:
Destroy(InstantiatedProjectileLeft.gameObject);