Unity 3D学习日记(4)

Day4

1.代码优化

2.发布(File-Build Settings)

3.添加天空盒子
导入包(Assets-Import Package-Skyboxes)
Window-Lighting

4.碰撞检测函数 void OnCollisionEnter(){

}

5.if(GUI.Button(new Rect(位置,位置,长,宽),”String Text”)))

6.调整场景发布顺序:从上而下(0 1 2 3…)

void Update () {
        GameObject.Find ("Canvas/Text").GetComponent().text="Instruction\n W:Forward, A:Left, S:Back, D:Right \nDestroy 6 items\nDon't touch the Capsule!";

    }

    void OnGUI(){
        if (GUI.Button (new Rect (400, 300, 100, 60), "Start")) {
            Application.LoadLevel ("01");

        }
        if (GUI.Button (new Rect (600, 300, 100, 60), "Quit")) {
            Application.Quit ();
        }

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SheXiangJi : MonoBehaviour {
    public Transform newobject;
    // Use this for initialization
    void Start () {
    }
    public int speed =5;
    public int NOB=0;         //Number of bullet
    public int NOD=0;         //Number of destroyed brick
    // Update is called once per frame
    void Update () {
        //"Camera translate"
        float x = Input.GetAxis ("Horizontal") * Time.deltaTime * speed;
        float z= Input.GetAxis ("Vertical") * Time.deltaTime * speed;
        transform.Translate (x,0,z);
        //print (x);

        //Shoot bullet
        if (Input.GetButtonDown ("Fire1")) {
            Transform n = Instantiate (newobject,transform.position,transform.rotation);
            Vector3 fwd = transform.TransformDirection (Vector3.forward);
            n.GetComponent().AddForce (fwd*5000);
            //Number of bullet
            NOB++;
            GameObject.Find ("Canvas/NOB").GetComponent ().text = "Number of bullet: " + NOB + " Number of destroyed brick:" + NOD;
        }

        //Rotate in Y using Q and E
        if(Input.GetKey(KeyCode.Q)){
            transform.Rotate (0,-25*Time.deltaTime,0,Space.Self);
        }
        if(Input.GetKey(KeyCode.E)){
            transform.Rotate (0,25*Time.deltaTime,0,Space.Self);
        }

        //Rotate in X using Z and C
        if(Input.GetKey(KeyCode.Z)){
            transform.Rotate (-25*Time.deltaTime,0,0,Space.Self);
        }
        if(Input.GetKey(KeyCode.C)){
            transform.Rotate (25*Time.deltaTime,0,0,Space.Self);
        }

        //Raise or down camera
        if (Input.GetKey (KeyCode.H)) {
            transform.Translate (0,speed*Time.deltaTime,0);
        }
        if (Input.GetKey (KeyCode.N)) {
            transform.Translate (0,-speed*Time.deltaTime,0);
        }
    }
}

你可能感兴趣的:(unity3d)