搜索
您的当前位置:首页正文

塔防简易游戏

来源:欧得旅游网

//创建20个敌人脚本

using UnityEngine;
using System.Collections;

public class homework05_enemyCreator : MonoBehaviour {

    public Transform[] spawnPoints;

    public GameObject enemyPrefab;
    int count;
	// Use this for initialization
	void Start () {
	
	}
	
    void AnimationEventTrigger()
    {
        if(count<=20)
        {
            count++;
            GameObject enemy = GameObject.Instantiate(enemyPrefab);
            Transform point = spawnPoints[Random.Range(0, 3)];
            enemy.transform.position = point.position;
            enemy.transform.forward = point.right;
        }
    }
}

//子弹脚本

using UnityEngine;
using System.Collections;

public class homework05_01 : MonoBehaviour {

    public float speed = 10;
    public GameObject bulletPrefab;

    CharacterController controller;
	// Use this for initialization
	void Start () {
        controller = this.GetComponent<CharacterController>();

    }
	
	// Update is called once per frame
	void Update () {

        float dx = Input.GetAxis("Horizontal");

        controller.Move(transform.right * dx * speed * Time.deltaTime);

        if(Input.GetMouseButtonDown(0))
        {
            GameObject go = Instantiate(bulletPrefab);
            go.transform.position = transform.position;
            go.transform.rotation = transform.rotation;

            go.GetComponent<Rigidbody>().velocity = transform.forward * 10;
            Destroy(go,3);
        }
    }
}

//子弹击中敌人脚本

using UnityEngine;
using System.Collections;

public class homework05_01_bullet : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}

    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Enemy"))
        {
            other.GetComponent<homework05_01_enemy>().OnDead();
            Destroy(gameObject);
        }
    }
}

//游戏胜利与游戏结束脚本

using UnityEngine;
using System.Collections;

public class homework05_01_enemy : MonoBehaviour {

    public static int deadCount = 0;
    Animator anim;
	// Use this for initialization
	void Start () {
        anim = this.GetComponent<Animator>();
	}
	
    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            Debug.Log("游戏结束");
            Time.timeScale = 0;
        }
    }

    //子弹调用的敌人死亡方法
    public void OnDead()
    {
        anim.SetBool("dying", true);
        this.GetComponent<Collider>().enabled = false;

        Destroy(gameObject,3);
        deadCount++;
        if(deadCount>=20)
        {
            Debug.Log("游戏胜利");
        }
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

热门图文

Top