类似愤怒小鸟的飞行弹道

发表于2017-10-18
评论0 3.2k浏览

下面和大家分享的是一个类似愤怒小鸟的飞行弹道的实现案例,在介绍之前大家需要了解一些基本的原理。


抛体运动的类型: 
       "很多子弹不仅垂直运动而且追随着水平的运动。那就,当他们向上移动或向下运动时也正在水平方向移动。弹体的运动 — — 横向和纵向


运动的两个组成部分。 
垂直运动: 
       在垂直运动,重力作用在物体上,并给予负加速度"-9.8 m/s²"(重心加速度)。这意味着物体的速度在每一秒减小-9.8 米/² 。自由落体的速度是 V = g * t。 如果我们有初始速度那么,物体下落速度方程: V = Vi g * t 加速度是-9.8 m/s²,在做自由落体时距离的计算方程 ;S= 1/2 * g * t * t ;考虑对象的初始速度情况下的

公式计算 ;S = Vi * t - 1/2 * g * t * t ;距离被减去,因为 g 的方向是向下。 
横向运动: 
       在水平运动,没有外力作用在水平方向匀速运动。因而在此基础上,是恒定的速度的 X 分量,在 X 方向的加速度为零。下面给出了用于计算距离和速度方程。S = v * t ; 下面是简单的 c# 代码,将显示球的弹道路径时它会沿着路径抛出。 
       注: 添加如下脚本到枪炮对象上。 创建 prefebs 球和轨迹点将运行时实例化。 球必须有Collider和Rigidbody。 
截图:

        


 

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. public class CannonScript : MonoBehaviour   
  6. {  
  7. // TrajectoryPoint and Ball will be instantiated  
  8.     public GameObject TrajectoryPointPrefeb;  
  9.     public GameObject BallPrefb;  
  10.       
  11.     private GameObject ball;  
  12.     private bool isPressed, isBallThrown;  
  13.     private float power = 25;  
  14.     private int numOfTrajectoryPoints = 30;  
  15.     private List trajectoryPoints;  
  16.     //---------------------------------------      
  17.     void Start ()  
  18.     {  
  19.         trajectoryPoints = new List();  
  20.         isPressed = isBallThrown =false;  
  21. //   TrajectoryPoints are instatiated  
  22.         for(int i=0;i<numOfTrajectoryPoints;i )  
  23.         {  
  24.             GameObject dot= (GameObject) Instantiate(TrajectoryPointPrefeb);  
  25.             dot.renderer.enabled = false;  
  26.             trajectoryPoints.Insert(i,dot);  
  27.         }  
  28.     }  
  29.     //---------------------------------------      
  30.     void Update ()   
  31.     {  
  32.         if(isBallThrown)  
  33.             return;  
  34.         if(Input.GetMouseButtonDown(0))  
  35.         {  
  36.             isPressed = true;  
  37.             if(!ball)  
  38.                 createBall();  
  39.         }  
  40.         else if(Input.GetMouseButtonUp(0))  
  41.         {  
  42.             isPressed = false;  
  43.             if(!isBallThrown)  
  44.             {  
  45.                 throwBall();  
  46.             }  
  47.         }  
  48.     // when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.  
  49.         if(isPressed)  
  50.         {  
  51.             Vector3 vel = GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition));  
  52.             float angle = Mathf.Atan2(vel.y,vel.x)* Mathf.Rad2Deg;  
  53.             transform.eulerAngles = new Vector3(0,0,angle);  
  54.             setTrajectoryPoints(transform.position, vel/ball.rigidbody.mass);  
  55.         }  
  56.     }  
  57.     //---------------------------------------      
  58.     // Following method creates new ball  
  59.     //---------------------------------------      
  60.     private void createBall()  
  61.     {  
  62.         ball = (GameObject) Instantiate(BallPrefb);  
  63.         Vector3 pos = transform.position;  
  64.         pos.z=1;  
  65.         ball.transform.position = pos;  
  66.         ball.SetActive(false);  
  67.     }  
  68.     //---------------------------------------      
  69. // Following method gives force to the ball  
  70.     //---------------------------------------      
  71.     private void throwBall()  
  72.     {  
  73.         ball.SetActive(true);      
  74.         ball.rigidbody.useGravity = true;  
  75.         ball.rigidbody.AddForce(GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition)),ForceMode.Impulse);  
  76.         isBallThrown = true;  
  77.     }  
  78.     //---------------------------------------      
  79. // Following method returns force by calculating distance between given two points  
  80.     //---------------------------------------      
  81.     private Vector2 GetForceFrom(Vector3 fromPos, Vector3 toPos)  
  82.     {  
  83.         return (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y))*power;  
  84.     }  
  85.     //---------------------------------------      
  86.     // Following method displays projectile trajectory path. It takes two arguments, start position of object(ball) and initial velocity of object(ball).  
  87.     //---------------------------------------      
  88.     void setTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )  
  89.     {  
  90.         float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x)   (pVelocity.y * pVelocity.y));  
  91.         float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));  
  92.         float fTime = 0;  
  93.           
  94.         fTime  = 0.1f;  
  95.         for (int i = 0 ; i < numOfTrajectoryPoints ; i )  
  96.         {  
  97.             float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);  
  98.             float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);  
  99.             Vector3 pos = new Vector3(pStartPosition.x   dx , pStartPosition.y   dy ,2);  
  100.             trajectoryPoints[i].transform.position = pos;  
  101.             trajectoryPoints[i].renderer.enabled = true;  
  102.             trajectoryPoints[i].transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude)*fTime,pVelocity.x)*Mathf.Rad2Deg);  
  103.             fTime  = 0.1f;  
  104.         }  
  105.     }  
  106. }  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: