团结使导弹遵循给定的轨迹

时间:2018-11-14 14:29:49

标签: unity3d

我正在制作一款可以向坦克发射导弹的游戏。我希望导弹遵循这一轨迹:

https://imgur.com/a/bRQ44zq

我尝试了几件事,但是没有运气。有谁知道如何实现这一轨迹?

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您应搜索:弹道,加农炮,弹道物理,抛物线等。

以下是在变换时投篮的示例: https://answers.unity.com/questions/148399/shooting-a-cannonball.html

我现在没有打开Unity,所以我不能给您完整的代码。顺便说一句,尝试更改以下代码供您自己使用,并在注释中告知我。

 function BallisticVel(target: Transform, angle: float): Vector3 {
     var dir = target.position - transform.position;  // get target direction
     var h = dir.y;  // get height difference
     dir.y = 0;  // retain only the horizontal direction
     var dist = dir.magnitude ;  // get horizontal distance
     var a = angle * Mathf.Deg2Rad;  // convert angle to radians
     dir.y = dist * Mathf.Tan(a);  // set dir to the elevation angle
     dist += h / Mathf.Tan(a);  // correct for small height differences
     // calculate the velocity magnitude
     var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
     return vel * dir.normalized;
 }

 var myTarget: Transform;  // drag the target here
 var cannonball: GameObject;  // drag the cannonball prefab here
 var shootAngle: float = 30;  // elevation angle

 function Update(){
     if (Input.GetKeyDown("b")){  // press b to shoot
         var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
         ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
         Destroy(ball, 10);
     }
 }