Unity 以一定角度启动刚体
Unity Launch Rigidbody at Angle
我希望以 30˚ 的角度和 30 m/s 的速度发射我的 Arrow GameObject。在脚本中,我向 Arrow 添加了一个刚体。但是,我也在尝试在 3D 场景 中朝玩家的方向(远离敌人)发射这个 Arrow。我不知道如何插入这些变量以获得 "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 = /*????*/;
感谢您的时间和耐心:)
使用一些几何学,知道向量的大小 (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);
假设你只拍摄 'forward' 你可以使用简化的:
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);
我希望以 30˚ 的角度和 30 m/s 的速度发射我的 Arrow GameObject。在脚本中,我向 Arrow 添加了一个刚体。但是,我也在尝试在 3D 场景 中朝玩家的方向(远离敌人)发射这个 Arrow。我不知道如何插入这些变量以获得 "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 = /*????*/;
感谢您的时间和耐心:)
使用一些几何学,知道向量的大小 (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);
假设你只拍摄 'forward' 你可以使用简化的:
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);