您的位置:首页 > 移动开发 > Unity3D

unity中虚拟直升机、直升机的旋转移动,子弹的实例化爆炸等效果的实现

2015-02-02 00:18 796 查看
一、物体的实例化:设置预设物体

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 (0, 0, 1), 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);
    }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐