在我制作的游戏中,玩家可以射击,我希望在玩家射击时让炮弹持续旋转。每个玩家都有一个名为SpawBala的空游戏对象,它会实例化炮弹。
我正在使用这些代码行来实例化它,但旋转不起作用:
//instantiates the bullet
GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
if (SpawBala.tag == "Bala1" && gameManager.playerTurn == 1)
{
BalaRigid.AddForce(Vector3.forward * ProjetilVeloc);
BalaRigid.transform.Rotate(new Vector3(Bulletspeed * Time.deltaTime, 0, 0));
gameManager.PontoAcaop1--;
}
我该怎么解决?
对Rotate
的调用只发生一次,并将对象旋转给定的量。
无论如何,由于有Rigidbody
在发挥作用,您根本不应该通过transform
组件设置变换,因为它破坏了物理。
而是始终通过Rigidbody
组件。
在生成子弹时,我实际上不会使用AddForce
和btwAddTorque
进行旋转。为了获得所需的速度,你必须根据子弹的重量计算所需的力。
相反,设置固定的初始CCD_ 7和用于旋转的CCD_。
还要注意的是,无论SpawnBala
对象在空间中的方向如何,当前您总是在全局世界空间Z方向上拍摄。您应该使用SpawnBala.transform.forward
作为方向向量:
BalaRigid.velocity = SpawnBala.transform.forward * ProjetilVeloc;
// Note that your variable names are confusing
// You should probably rename it to BulletRotationSpeed e.g.
BalaRigid.angularVelocity = SpawnBala.transform.right * Bulletspeed;