Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 射弹未正确继承父速度(Unity3D)_C#_Unity3d_Game Physics - Fatal编程技术网

C# 射弹未正确继承父速度(Unity3D)

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

我试图从一个高速移动的玩家身上发射射弹。问题是子弹似乎没有继承角速度。如果玩家以500 U/s的速度直线移动,则子弹将正确继承该速度并将其自身速度添加到该速度上:

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()移动播放器,但无法使用子弹产生任何结果


直接的问题是,在创建和发射弹丸之前,弹丸不属于物理模拟的一部分,这意味着玩家旋转到该点将无法影响其移动。我想,如果你一开始就制造出射弹,用一个薄弱的接头将其连接到枪上,然后在发射时断开接头并增加其速度,这可能会起作用——但如果你只是“伪造”它,情况会更简单

应用圆周运动 这类似于物理教科书中的经典圆周运动示例。当你的子弹围绕玩家(枪内)旋转时,它的正常路径(如果释放)将与玩家周围的圆形路径相切。因此,当子弹被发射时(从而从圆形路径释放),玩家的角速度将转化为子弹的线速度。下面是一个简单的图表,我把它放在一起,表示一个球在绳子上旋转成一个圆圈的情况:

求子弹的切向速度 你不想在发射后对射弹施加任何角速度——正如你所看到的,这只会使射弹沿着自己的轴旋转。相反,您希望对投射物应用一个附加的速度向量,该向量与玩家的旋转相切(并垂直于子弹面)。那么我们如何做到这一点呢

根据,在圆周上旋转的物体上任意点的切向速度公式是:

速度=角度速度x半径

需要记住的一点是,角速度是以弧度为单位的,而不是以度为单位的

那么,让我们把这些放在一起。以下是如何编写此代码的一般想法:

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切向速度;
}

希望这有帮助!当你学习游戏物理时,阅读一些基本的运动学/力学从来都不是一个坏主意,因为有时依赖游戏物理实际上比设计自己的解决方案更复杂。

如果将子弹定义为枪的子对象,那么如果枪移动和/或旋转,那么子弹也会相对于枪移动。我认为这不是一个好主意。这是一个很好的答案。我还发现,将繁殖点的角速度角乘以玩家的旋转角度效果很好,这两种解决方案是相似的。