C# 射弹未正确继承父速度(Unity3D)
我试图从一个高速移动的玩家身上发射射弹。问题是子弹似乎没有继承角速度。如果玩家以500 U/s的速度直线移动,则子弹将正确继承该速度并将其自身速度添加到该速度上:C# 射弹未正确继承父速度(Unity3D),c#,unity3d,game-physics,C#,Unity3d,Game Physics,我试图从一个高速移动的玩家身上发射射弹。问题是子弹似乎没有继承角速度。如果玩家以500 U/s的速度直线移动,则子弹将正确继承该速度并将其自身速度添加到该速度上: GameObject bullet = Instantiate(projectile, gun.transform.position, gun.transform.rotation); bullet.GetComponent<Rigidbody>().velocity = r.velocity + bullet.trans
GameObject bullet = Instantiate(projectile, gun.transform.position, gun.transform.rotation);
bullet.GetComponent<Rigidbody>().velocity = r.velocity + bullet.transform.forward *bulletSpeed;
GameObject bullet=实例化(投射物,gun.transform.position,gun.transform.rotation);
bullet.GetComponent().velocity=r.velocity+bullet.transform.forward*bulletSpeed;
但是,如果玩家的旋转速度非常快,它不会继承旋转速度,子弹看起来会转向一边
我如何实现这一点?我尝试将玩家的角速度指定给子弹,但这只会导致子弹在同一方向移动时旋转
一些额外的细节:
- 我正在使用rigidbody.MoveRotation()旋转我的播放器,但我手动计算了angularVelocity以将其分配给子弹
- 我尝试使用AddTorque()移动播放器,但无法使用子弹产生任何结果
FireGun() {
// Assumes the current class has access to the rotation rate of the player
float bulletAngularVelocityRad = Mathf.Deg2Rad(rotationRateDegrees)
// The radius of the circular motion is the distance between the bullet
// spawn point and the player's axis of rotation
float bulletRotationRadius =
(r.transform.position - gun.transform.position).magnitude;
GameObject bullet =
Instantiate(projectile, gun.transform.position, gun.transform.rotation);
// You may need to reverse the sign here, since bullet.transform.right
// may be opposite of the rotation
Vector3 bulletTangentialVelocity =
bulletAngularVelocityRad * bulletRotationRadius * bullet.transform.right;
// Now we can just add on bulletTangentialVelocity as a component
// vector to the velocity
bullet.GetComponent<Rigidbody>().velocity =
r.velocity + bullet.transform.forward * bulletSpeed + bulletTangentialVelocity;
}
FireGun(){
//假设当前类可以访问玩家的旋转速率
浮点数角速度拉德=数学Deg2Rad(旋转速率度)
//圆周运动的半径是子弹之间的距离
//繁殖点和玩家的旋转轴
浮动项目符号旋转半径=
(r.transform.position-枪。transform.position)。大小;
游戏物体子弹=
实例化(射弹,枪.变换.位置,枪.变换.旋转);
//您可能需要在此处反转符号,因为bullet.transform.right
//可能与旋转方向相反
矢量切向速度=
bulletAngularVelocityRad*bulletRotationRadius*bullet.transform.right;
//现在我们可以把BullettinealVelocity作为一个组件添加进去
//速度矢量
bullet.GetComponent().velocity=
r、 速度+bullet.transform.forward*bullet速度+bullet切向速度;
}
希望这有帮助!当你学习游戏物理时,阅读一些基本的运动学/力学从来都不是一个坏主意,因为有时依赖游戏物理实际上比设计自己的解决方案更复杂。如果将子弹定义为枪的子对象,那么如果枪移动和/或旋转,那么子弹也会相对于枪移动。我认为这不是一个好主意。这是一个很好的答案。我还发现,将繁殖点的角速度角乘以玩家的旋转角度效果很好,这两种解决方案是相似的。