[英]Projectile trajectory based on angle
在我的 3D 游戏(自上而下的射击游戏)中,我尝试这样做:当您在 map 的特定区域上单击鼠标按钮(点上光线投射)时 -> 从枪中射出一枪 -> 弹丸飞入具有一定路径且与鼠标指针距离相同的枪管方向(但不是方向)。 我已经有了计算弹丸轨迹的源代码。 帮助使射弹沿枪管的方向飞行,但使用鼠标位置射线投射显示的距离,而不是鼠标位置方向。
```private void FireCannonAtPoint(Vector3 point)
{
var velocity = BallisticVelocity(point, _angle);
GameObject projectile = Instantiate(_projectile, _shootPoint.transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody>().velocity = velocity;
}
private Vector3 BallisticVelocity(Vector3 destination, float angle)
{
Vector3 dir = destination - _shootPoint.transform.position;
float height = dir.y;
dir.y = 0;
float dist = dir.magnitude;
float a = angle * Mathf.Deg2Rad;
dir.y = dist * Mathf.Tan(a);
dist += height / Mathf.Tan(a);
float velocity = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return velocity * dir.normalized;
}```
枪管:3D 中的段[turret_end_1, turret_end2]
,其中turret_end_1
是炮塔上子弹离开的点。
屏幕:焦点F
和屏幕平面,命名为screen_plane
。
# From 3D to 2D: projection from 3D space on the screen_plane:
Mouse = [mouse_x, mouse_y] # given on the screen_plane
B1 = project_on_screen_plane(turret_end_1)
B2 = project_on_screen_plane(turret_end_2)
# Calculations in 2D, i.e. on the screen_plane
U = B1-B2
U = ( dot_product(Mouse-B1, U) / dot_product(U, U) ) * U
U = B1 + U # this is the point you are looking for on the screen
# From 2D back to 3D: connect the focal point with the point U on the screen plane
# to form a line and find its intersection point in 3D with the line B1 B2
# aligned with the turret.
# line through F with directing vector Vector_from_F_to(U)
# line through B1 with directing vector B1-B2
V = intersect( F, Vector_from_F_to(U), B1, B1-B2 ) # the point you are looking for in 3D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.