类似愤怒小鸟的飞行弹道




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

运动的两个组成部分。
垂直运动:
             在垂直运动,重力作用在物体上,并给予负加速度"-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。
截图:

       


 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CannonScript : MonoBehaviour 
{
// TrajectoryPoint and Ball will be instantiated
    public GameObject TrajectoryPointPrefeb;
    public GameObject BallPrefb;
    
    private GameObject ball;
    private bool isPressed, isBallThrown;
    private float power = 25;
    private int numOfTrajectoryPoints = 30;
    private List trajectoryPoints;
    //---------------------------------------    
    void Start ()
    {
        trajectoryPoints = new List();
        isPressed = isBallThrown =false;
//   TrajectoryPoints are instatiated
        for(int i=0;i<numOfTrajectoryPoints;i++)
        {
            GameObject dot= (GameObject) Instantiate(TrajectoryPointPrefeb);
            dot.renderer.enabled = false;
            trajectoryPoints.Insert(i,dot);
        }
    }
    //---------------------------------------    
    void Update () 
    {
        if(isBallThrown)
            return;
        if(Input.GetMouseButtonDown(0))
        {
            isPressed = true;
            if(!ball)
                createBall();
        }
        else if(Input.GetMouseButtonUp(0))
        {
            isPressed = false;
            if(!isBallThrown)
            {
                throwBall();
            }
        }
    // when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.
        if(isPressed)
        {
            Vector3 vel = GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition));
            float angle = Mathf.Atan2(vel.y,vel.x)* Mathf.Rad2Deg;
            transform.eulerAngles = new Vector3(0,0,angle);
            setTrajectoryPoints(transform.position, vel/ball.rigidbody.mass);
        }
    }
    //---------------------------------------    
    // Following method creates new ball
    //---------------------------------------    
    private void createBall()
    {
        ball = (GameObject) Instantiate(BallPrefb);
        Vector3 pos = transform.position;
        pos.z=1;
        ball.transform.position = pos;
        ball.SetActive(false);
    }
    //---------------------------------------    
// Following method gives force to the ball
    //---------------------------------------    
    private void throwBall()
    {
        ball.SetActive(true);    
        ball.rigidbody.useGravity = true;
        ball.rigidbody.AddForce(GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition)),ForceMode.Impulse);
        isBallThrown = true;
    }
    //---------------------------------------    
// Following method returns force by calculating distance between given two points
    //---------------------------------------    
    private Vector2 GetForceFrom(Vector3 fromPos, Vector3 toPos)
    {
        return (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y))*power;
    }
    //---------------------------------------    
    // Following method displays projectile trajectory path. It takes two arguments, start position of object(ball) and initial velocity of object(ball).
    //---------------------------------------    
    void setTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )
    {
        float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));
        float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));
        float fTime = 0;
        
        fTime += 0.1f;
        for (int i = 0 ; i < numOfTrajectoryPoints ; i++)
        {
            float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
            float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
            Vector3 pos = new Vector3(pStartPosition.x + dx , pStartPosition.y + dy ,2);
            trajectoryPoints[i].transform.position = pos;
            trajectoryPoints[i].renderer.enabled = true;
            trajectoryPoints[i].transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude)*fTime,pVelocity.x)*Mathf.Rad2Deg);
            fTime += 0.1f;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值