Space Shoot教程学习笔记

Space Shoot教程学习笔记

环境:Unity3D 5.0.1f1

视频教程地址
https://unity3d.com/learn/tutorials/projects/space-shooter/setting-up-the-project

注意事项
[1]完成Space Shoot教程所需要的Assets在官网很难download,我们只能在其它地点下到这个资源,然后导入。
这里假设,我已经下载到了”Unity Projects Space Shooter.unitypackage”文件,
新建Unity3D工程后,进入主菜单->[Assets]->[Import Package]->[All]->[Custom Package…]->[Importing package]
->[I Make a Backup, Go Ahead!]进行导入。

[2]主菜单[Edit]->[Render Settings]被移到了[Window]->[Lighting]下面

[3]
使用“rigidbody.velocity = movement”会出错,得用下面的代码替换。
GetComponent().velocity = movement;

[4]Documentation ,[Ctrl]+[‘]

[5]关于”yield”方法。
[5-1]首先是所有update方法先都运行完才运行yield后面的内容,
就像有个update总管把所有的这一帧的update方法集合起来先搞定这一帧所有update方法,
然后把所有的欠下的yield后面的内容执行掉,就接着运行下一帧。
[5-2]只能在协同程序里面使用。例如你不能在Update和FixedUpdate中调用“yield”方法。
[5-3]你可以在Start方法中启动“协同程序”.
void Start ()
{
StartCoroutine (你的方法名());
}
[5-4]如何使用”yield”方法的例子
http://zhidao.baidu.com/link?url=jXHSl7LFQ3RQvMQHXkAfQCYmY0IjPXtRPJNIZYdzHcqaggh4BH_88h5SDYH74lJnE8uxTUo7IwQPmOYUXWZGehRT3PGx5j50M5VWFDQn6ZW

[6]使用Debug.Log可以在程序运行的时候,打印出调试信息。

[7]
audio.Play()改为下面这条代码
GetComponent().Play ();

源码清单
DestroyByBoundary.cs

using UnityEngine;
using System.Collections;

public class DestroyByBoundary : MonoBehaviour {
    void OnTriggerExit(Collider other)
    {
        Destroy(other.gameObject);
    }
}

DestroyByContact.cs

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour {
    public GameObject explosion;
    public GameObject playerExplosion;
    public int scoreValue;
    private GameController gameController;

    void Start ()
    {
        GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent <GameController>();
        }
        if (gameController == null)
        {
            Debug.Log ("Cannot find 'GameController' script");
        }
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.tag == "Boundary")
        {
            return;
        }
        Instantiate(explosion, transform.position, transform.rotation);
        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.GameOver ();
        }
        gameController.AddScore (scoreValue);
        Destroy(other.gameObject);
        Destroy(gameObject);
    }
}

DestroyByTime.cs

using UnityEngine;
using System.Collections;

public class DestroyByTime : MonoBehaviour {
    public float lifetime;

    void Start ()
    {
        //after lifetime destroy the gameObject
        Destroy (gameObject, lifetime);
    }
}

GameController.cs

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
        public GameObject hazard;
        public Vector3 spawnValues;
        public int hazardCount;
        public float spawnWait;
        public float startWait;
        public float waveWait;

        public GUIText scoreText;
        public GUIText restartText;
        public GUIText gameOverText;

        private bool gameOver;
        private bool restart;
        private int score;

        void Start ()
        {
            gameOver = false;
            restart = false;
            restartText.text = "";
            gameOverText.text = "";
            score = 0;
            UpdateScore ();
            StartCoroutine (SpawnWaves ());
        }

        void Update ()
        {
            if (restart)
            {
                if (Input.GetKeyDown (KeyCode.R))
                {
                    Application.LoadLevel (Application.loadedLevel);
                }
            }
        }

        IEnumerator SpawnWaves ()
        {
            yield return new WaitForSeconds (startWait);
            while (true)
            {
                for (int i = 0; i < hazardCount; i++)
                {
                    Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                    Quaternion spawnRotation = Quaternion.identity;
                    Instantiate (hazard, spawnPosition, spawnRotation);
                    yield return new WaitForSeconds (spawnWait);
                }
                yield return new WaitForSeconds (waveWait);

                if (gameOver)
                {
                    restartText.text = "Press 'R' for Restart";
                    restart = true;
                    break;
                }
            }
        }

        public void AddScore (int newScoreValue)
        {
            score += newScoreValue;
            UpdateScore ();
        }

        void UpdateScore ()
        {
            scoreText.text = "Score: " + score;
        }

        public void GameOver ()
        {
            gameOverText.text = "Game Over!";
            gameOver = true;
        }
}

Mover.cs

using UnityEngine;
using System.Collections;

public class Mover : MonoBehaviour {

    public float speed;

    void Start ()
    {
        GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
}

PlayerController.cs

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin,xMax,zMin,zMax;
}

public class PlayerController : MonoBehaviour {

    public float Speed;
    public Boundary boundary;
    public float tilt;

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;

    float nextFire;

    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            GetComponent<AudioSource>().Play ();
        }
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, .0f, moveVertical);
        GetComponent<Rigidbody>().velocity = movement * Speed;
        GetComponent<Rigidbody>().position = new Vector3
        (
                Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
                .0f,
                Mathf.Clamp(GetComponent<Rigidbody>().position.z,boundary.zMin, boundary.zMax)
        );

        GetComponent<Rigidbody> ().rotation = Quaternion.Euler(.0f, .0f, GetComponent<Rigidbody>().velocity.x*-tilt);
    }
}

RandomRotator.cs

using UnityEngine;
using System.Collections;

public class RandomRotator : MonoBehaviour {
    public float tumble;

    void Start ()
    {
        GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble; 
    }
}

你可能感兴趣的:(unity3d)