【问题标题】:Unity Launch Rigidbody at AngleUnity 以角度发射刚体
【发布时间】:2018-05-17 01:23:11
【问题描述】:

我希望以 30˚ 的角度和 30 m/s 的速度发射我的 箭头 GameObject。在一个脚本中,我为这个箭头添加了一个刚体。但是,我也尝试在 3D 场景 中向玩家的方向(远离敌人)发射此箭头。我不知道如何插入这些变量以获取“arrowRigidbody.velocity”的 Vector3

//THE VARIABLES REFERENCED ABOVE APPEAR LIKE SO:
Rigidbody arrowRigidbody;
Transform playerTransform;
Transform enemyTransform;
float angle = 30f;
float velocity = 30f;

//How do I use these variables in order to shoot the projectile at a speed
//of 30 m/s and an angle of 30˚ in the direction of the player in 3D scene
arrowRigidbody.velocity = /*????*/;

感谢您的时间和耐心:)

【问题讨论】:

    标签: c# unity3d game-physics rigid-bodies


    【解决方案1】:

    使用一些几何,知道向量的大小 (m) 为 1,y 分量将为 m/2,x 分量将为 m*(3^.5)/2。这将使您的最终价值:

    arrowRigidbody.velocity = new Vector2(Mathf.Pow(3, .5f)/2, 1/2) * velocity;
    

    对于变化的角度,您知道 x 分量将是 m * cos(angle),y 分量将是 m * sin(angle),剩下的就是:

    float velx = velocity * Mathf.Cos(angle * Mathf.Deg2Rad);
    float vely = velocity * Mathf.Sin(angle * Mathf.Deg2Rad);
    arrowRigidbody.velocity = new Vector2(velx, vely);
    

    【讨论】:

    • 如果角度不断变化,我将如何实现它以根据变化的角度改变速度?
    • 我可以复制并粘贴 Z 轴的速度计算吗?
    • 没有意识到这是在 3d 中......但这不应该影响速度。我认为这只是你的播放器的 z 角
    • 哦,那是真的,对不起。谢谢!
    • 这终于让我正确地根据弹丸的追踪轨迹将速度应用于刚体。呸! csharp _RigidBody.velocity = new Vector3(_velocity * Mathf.Cos(_angleInDegrees * Mathf.Deg2Rad), _velocity * Mathf.Sin(_angleInDegrees * Mathf.Deg2Rad), 0); 谢谢!
    【解决方案2】:

    假设你只“向前”射击,你可以使用简化:

        var targetDirn = transform.forward;
        var elevationAxis = transform.right;
    
        var releaseAngle = 30f;
        var releaseSpeed = 30f;
    
        var releaseVector = Quaternion.AngleAxis(releaseAngle, elevationAxis) * targetDirn;
        arrowRigidbody.velocity = releaseVector * releaseSpeed;
    

    如果需要拍摄'off-axis',可以替换前两行:

        var targetDirn = (target.transform.position - transform.position).normalized;
        var elevationAxis = Vector3.Cross(targetDirn, Vector3.up);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 2016-11-28
      • 1970-01-01
      相关资源
      最近更新 更多