unity中虛擬直升機、直升機的旋轉移動,子彈的實例化爆炸等效果的實現

一、物體的實例化:設置預設物體

1、        首先把要做預設的物體預設

2、        創建一個空物體

3、        把實例化的腳本綁定在空物體上

4、        Targer目標物體綁定

5、        把空物體綁定在產生的位置



using UnityEngine;
using System.Collections;

public class ShiLiHua : MonoBehaviour
{
        public GameObject Targer;
        //public Transform zuobiao;
        // 這樣就可以直接拿到transform組件
    
        void Start ()
        {

                //GameObject.Instantiate (Targer, zuobiao.position, zuobiao.rotation);

        }
    
        void Update(){
                if(Input.GetKeyDown("space")){

//實例化函數Instantiate
                GameObject.Instantiate (Targer, this.transform.position, this.transform.rotation);
                }
        }
    
}
 

 

直升機做成預支物體就可以導出,以備以後使用。

 

二、移動子彈:

void Update ()
        {       //使飛機的子彈每秒向前移動多少的距離
                this.transform.Translate (new Vector3 (001), Space.Self);
        }

 

三、碰撞時產生爆炸的效果

void OnCollisionEnter (Collision other)
        {
                // 碰撞的第一個接觸點
                GameObject.Instantiate (Boom, other.contacts [0].point, this.transform.rotation);
                Destroy (this.gameObject);
        }

通過Input輸入使飛機的螺旋槳旋轉

public float r

void Update () {

r = (Input.GetAxis ("Vertical"))*44;
        this.transform.Rotate (r,0,0);

}

五、通過Input輸入使飛機上下左右前後的移動並旋轉:

using UnityEngine;
using System.Collections;

public class AddFoce_AirPlane : MonoBehaviour {
    public float mg;

    public float zuoyou;
    public float shangxia;
    public float qianhou;
    public float xuanzhuan;


    public Vector3 rotate_Air;
    public Vector3 force;
    public Vector3 forcezy;

    public Vector3 qian;
    public Vector3 zuo;
        public GameObject Boom;

    

    // Use this for initialization
    void Start () {
        mg = this.rigidbody.mass * 9.81f;
        rotate_Air = new Vector3 (0,0,0);

    }
    

    void Update () {



        zuoyou = Input.GetAxis ("Vertical");//zuoyou
        shangxia = Input.GetAxis ("Horizontal1");//shangxia
        qianhou = Input.GetAxis ("Horizontal");//qianhou
        xuanzhuan = Input.GetAxis ("Vertical1");//xuanzhuan

        qian = new Vector3 (this.transform.forward.x,0,this.transform.forward.z);//ba shi jie zuo biao zhong de Y gui 0;
        zuo = new Vector3 (this.transform.right.x,0,this.transform.right.z);

        rotate_Air.y += xuanzhuan;

        rotate_Air.x = qianhou * 30;
        force=qian*(30.5f * mg * Mathf.Tan (rotate_Air.x * Mathf.Deg2Rad));//jiao du zhuan hu du

        rotate_Air.z = -1*(zuoyou * 30);
        forcezy.x = 30.5f * mg * Mathf.Tan (rotate_Air.z * Mathf.Deg2Rad);//jiao du zhuan hu du

        this.transform.rotation = Quaternion.Euler (rotate_Air);//fan hui yi ge xuan zhuan jiao du



    }
    void FixedUpdate(){
        rigidbody.AddForce (0,mg,0);
        rigidbody.AddForce (0,mg*shangxia*10,0);
        rigidbody.AddForce (force);//tian jia yi ge xiang dui li shi feiji xuan zhuan zhihou yi ranxiang qianfei
        rigidbody.AddForce (-forcezy);
    }

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章