unity3D射击游戏

射击游戏

1.Enemy

using UnityEngine;

[AddComponentMenu("MyGame/Enemy")]
public class Enemy : MonoBehaviour
{
    public float speed = 3.0f;
    public float life = 10.0f;
    protected float rotSpeed = 30;
    internal Renderer renderer;
    internal bool isActive = false;

    private void Start()
    {
        renderer = this.transform.GetComponent<Renderer>();
    }

    private void OnBecameVisible()
    {
        isActive = true;
    }
    
    void Update()
    {
        updateMove();
        if (isActive && !this.renderer.isVisible)
        {
            Destroy(this.gameObject);
        }
    }

    public virtual void updateMove()
    {
        transform.Translate(new Vector3(Mathf.Sin(Time.time) * Time.deltaTime, 0, -speed * Time.deltaTime));
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == TagsManager.PLAYEROCKET)
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket != null)
            {
                life -= rocket.power;
                if (life <= 0)
                {
                    GameManager.instance.AddScore(20);
                    Destroy(this.gameObject);
                }
            }
        }
        else if (other.tag == TagsManager.PLAYER)
        {
            life = 0;
            Destroy(this.gameObject);
        }
    }
}

2.EnemyRocket

using UnityEngine;

public class EnemyRocket : Rocket
{
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == TagsManager.PLAYER || other.tag == TagsManager.PLAYEROCKET)
        {
            GameManager.instance.AddScore(1);
            Destroy(this.gameObject);
        }
    }
}

3.EnemySpawn

using System.Collections;
using UnityEngine;

public class EnemySpawn : MonoBehaviour
{
    public Transform[] ememyPrefab;
    
    void Start()
    {
        StartCoroutine(SpawnEnemy());
    }

    IEnumerator SpawnEnemy()
    {
        while (true)
        {
            yield return new WaitForSeconds(Random.Range(0, 15));
            Instantiate(ememyPrefab[Random.Range(0, ememyPrefab.Length)], this.transform.position,
                Quaternion.identity);
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawIcon(this.transform.position, "item.png", true);
    }
}

4.GameManager

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;

    private void Awake()
    {
        instance = this;
    }

    public int score = 0;

    public Text scoreText;
    public Text lifeText;

    public CanvasGroup gameover;
    
    void Start()
    {
        scoreText.text = string.Format("{0}", 0);
        lifeText.text = string.Format("{0}", Player.life);
        //gameover.alpha = 0;
        gameover.interactable = false;
        gameover.alpha = 0;
        gameover.blocksRaycasts = false;
    }
    
    public void AddScore(int point)
    {
        this.score += point;
        scoreText.text = string.Format("{0}", score);
    }

    public void ChangeLife(int life)
    {
        lifeText.text = string.Format("{0}", life);
        if (life <= 0)
        {
            gameover.interactable = true;
            gameover.alpha = 1;
            gameover.blocksRaycasts = true;
        }
    }

    public void OnButtonGameReStart()
    {
        SceneManager.LoadScene("level1");
        Player.life = 3;
    }
}

5.Player

using UnityEngine;

[AddComponentMenu("MyGame/Player")]
public class Player : MonoBehaviour
{
    public float speed = 6.0f;
    public Transform rocketBig;
    public Transform rocketSmall;

    public float rocketTime = 0;
    public static int life = 3;
    public AudioClip shootClip;
    private AudioSource audioSource;
    public Transform explosionFx;

    public Transform fire1;
    public Transform fire2;
    public Transform fire3;

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == TagsManager.ENEMY || other.tag == TagsManager.ENEMYROCKET)
        {
            life -= 1;
            GameManager.instance.ChangeLife(life);
            if (life <= 0)
            {
                Instantiate(explosionFx, this.transform.position, Quaternion.identity);
                Destroy(this.gameObject);
            }
        }
    }

    void Start()
    {
        audioSource = this.transform.GetComponent<AudioSource>();
    }

    void Update()
    {
        float movex = 0;
        float movey = 0;
        if (Input.GetKey(KeyCode.UpArrow))
        {
            movex += speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            movex -= speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            movey -= speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            movey += speed * Time.deltaTime;
        }

        if (this.transform.position.z + movex >= 9 || this.transform.position.z + movex <= -9 ||
            this.transform.position.x + movey >= 9 || this.transform.position.x + movey <= -9)
        {
            return;
        }

        this.transform.Translate(new Vector3(movey, 0, movex));
        rocketTime -= Time.deltaTime;
        if (rocketTime <= 0)
        {
            rocketTime = 0.1f;
            if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
            {
                Instantiate(rocketBig, this.transform.position, this.transform.rotation);
                audioSource.PlayOneShot(shootClip);
            }
            else if (Input.GetMouseButton(1))
            {
                Instantiate(rocketSmall, fire1.position, this.transform.rotation);
                Instantiate(rocketSmall, fire2.position, this.transform.rotation);
                Instantiate(rocketSmall, fire3.position, this.transform.rotation);
                audioSource.PlayOneShot(shootClip);
            }
        }
    }
}

6.Rocket

using UnityEngine;

[AddComponentMenu("MyGmme/Rocket")]
public class Rocket : MonoBehaviour
{
    public float speed = 6.0f;
    public float power = 1.0f;
    
    void Update()
    {
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
    }

    private void OnBecameInvisible()
    {
        if (this.enabled)
        {
            Destroy(this.gameObject);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == TagsManager.ENEMY || other.tag == TagsManager.ENEMYROCKET)
        {
            Destroy(this.gameObject);
        }
    }
}

7.SuperEnemy

using UnityEngine;

public class SuperEnemy : Enemy
{
    public Transform enemyRocket;
    public float fireCoolDown = 1.0f;
    private Transform player;

    public override void updateMove()
    {
        fireCoolDown -= Time.deltaTime;
        if (fireCoolDown <= 0)
        {
            fireCoolDown = 1.0f;


            if (player != null)
            {
                Vector3 relativePos = player.position - this.transform.position;
                Instantiate(enemyRocket, this.transform.position, Quaternion.LookRotation(relativePos));
            }

            GameObject temp = GameObject.FindGameObjectWithTag(TagsManager.PLAYER);
            if (temp != null)
            {
                player = temp.transform;
            }
        }

        transform.Translate(new Vector3(0, 0, -speed * Time.deltaTime));
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == TagsManager.PLAYEROCKET)
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket != null)
            {
                life -= rocket.power;
                if (life <= 0)
                {
                    GameManager.instance.AddScore(100);
                    Destroy(this.gameObject);
                }
            }
        }
        else if (other.tag == TagsManager.PLAYER)
        {
            life = 0;
            Destroy(this.gameObject);
        }
    }
}

8.TagsManager

public class TagsManager
{
    public static string PLAYER = "Players";
    public static string ROCKET = "Rocket";
    public static string ENEMYROCKET = "EnemyRocket";
    public static string PLAYEROCKET = "PlayerRocket";
    public static string ENEMY = "Enemy";
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值