unity的简易飞机大战

``
1、第一步把玩家、玩家打出的子弹、敌人、敌人打出的子弹的脚本创建出来。

2、第二步把玩家、玩家打出的子弹、敌人、敌人打出的子弹拖到场景中执行。

3、第三步就开始写脚本内容了

写脚本内容跟据需求第一步来写玩家的移动,还有控制玩家不出游戏界面,脚本如下:

private float speed = 10 ;//键盘控制的速度
    private float offSet = 10f;//飞船的偏移量
    private Rigidbody r;


    public GameObject bullet;//子弹
    public Transform pos;//弹口
    public AudioClip clip;//音频
 
    void Start () {

//获取刚体
        r = GetComponent<Rigidbody>();//这是获取刚体组件
    }
    
   
    void Update () {
        Move(); 

Fire();
       }

void Fire()
    {
        if (Input.GetButtonDown("Fire1") &&Time.time>nextTime)
        {
            nextTime = spacetime + Time.time;//下一颗子弹运行的时间=每颗子弹飞行的时间+游戏运行时间
            Instantiate(bullet, pos.position, Quaternion.identity);
        }
    }
  
void Move()
    {
        //用键盘来控制玩家飞机移动
        float x = Input.GetAxis("Horizontal")  * speed;
        float z = Input.GetAxis("Vertical") * speed;
        //transform.Translate(new Vector3(x, 0, z));
        r.velocity = new Vector3(x, 0, z);


        //设置边界值不让飞机飞出屏幕
        float xPos = Mathf.Clamp(transform.position.x, -6f, 6f);

//Mathf.Clamp(自身的x轴,边界的最小值,边界的最大值)
        float zPos = Mathf.Clamp(transform.position.z, -43f, -22f);
        transform.position = new Vector3(xPos, transform.position.y, zPos);


        //飞船的偏移
        r.rotation = Quaternion.Euler(0, 0, r.velocity.x * offSet * (-1));

//控制飞船的偏移公式为

//r.rotation=Quaternion.Euler(自身的x轴不变,自身的y轴不变,x轴的刚体*偏移量*(-1))
    }

玩家的子弹的克隆写在玩家身上脚本里比较好,子弹的脚本就给它专门写子弹的操作,子弹碰到陨石摧毁陨石产生爆炸声,子弹代码如下:

 private float speed = 2f;//控制速度
    public GameObject explosion;//爆炸声音
    void Start () {
        
    }
    void Update () {

//子弹向前飞的力
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
    void OnTriggerExit(Collider other)//触发器(出来的时候写这个触发器)
    {
        if (other.CompareTag("Border"))//子弹碰到你设置的一个边界触发器
        {
            Destroy(gameObject);//碰到了就把子弹摧毁
        }
    }
    void OnTriggerEnter(Collider other)//触发器(刚进这个触发器的时候)
    {
        if (other.CompareTag("Asteroid"))//碰到了陨石
        {
            Destroy(gameObject);//摧毁子弹
            Destroy(other.gameObject);//摧毁陨石
            GameObject go = Instantiate(explosion, other.gameObject.transform.position, Quaternion.identity);//克隆爆炸声音
            Destroy(go, 1f);

            GameManager.gm.score++;//这是积分器,是在后面代码中
        }
    }
   

再建一个专门管理陨石生成和血量和界面跳转等各种操作,代码如下:

我这陨石是用数组来存储的

public GameObject[] asteroids;//用数组来存储陨石
    public Text playerHpText, scoreText;//这个是要创建一个UI界面

//HideInInspector表示将原本显示在面板上的序列化值隐藏起来
    [HideInInspector]
    public int playerHp = 10;//这个是定义一个玩家的血量
    [HideInInspector]
    public int score = 0;
    public GameObject Win, gameOver;//这是个游戏的赢的界面和游戏结束的界面
//unity 单例模式
public static GameManager gm;

void Start()
{
    gm = this;//就是把本类赋值给gm
    StartCoroutine(Print());
}

//用协程来克隆陨石,用死循环来每秒克隆陨石

IEnumerator Print()
{
    
    while (true)
    {
        yield return new WaitForSeconds(3f);
        for (int i = 0; i < asteroids.Length; i++)
        {

           // 用到随机数来克隆陨石的位置
            int x = Random.Range(-6, 6);//x轴随机
            int z = Random.Range(-21, -22);
            Vector3 pos = new Vector3(x, 0.28f, z);
            Instantiate(asteroids[i], pos, Quaternion.identity);
            
        }
         yield return new WaitForSeconds(6f);
    }

    //}

}

void Update () {
    if (score==10)
    {
        Time.timeScale = 0;
        Win.SetActive(true);
    }
    if (playerHp==0)
    {
        Time.timeScale = 0;
        gameOver.SetActive(true);
    }
    playerHpText.text = playerHp.ToString();
    scoreText.text = score.ToString();
}

//这个两个是按钮事件,达到跳转界面效果

public void GoToStart()
{
    SceneManager.LoadScene("start");
}
public void GoToGame2()
{
    SceneManager.LoadScene("Game2");
    //Time.timeScale = 1;//这个语句是用于游戏的暂停和开启的作用
}

接下来我们来做第二个界面的脚本

第二个脚本主要是来玩家飞机来打对方飞机的第二关,要求如下:
这个需求是要求我们需要用协程或者Invoke,或者是InvokeReapting来做,现在主要使用的是协程

在Start里面启动协程


private Rigidbody r;//加刚体
	private float spacetime = 1f;//每颗子弹间隔的时间
	private float nextTime;//下一颗子弹发射的时间
	private float moveSpeed = 2f;//移动速度
	private int enemyHp = 1;


	public GameObject bullet;//敌人子弹
	public Transform pos;//敌人发射子弹的炮口
	public AudioClip clip;//爆炸特效
	public GameObject exlopion;//敌机爆炸特效

	void Start()
	{
		r = GetComponent<Rigidbody>();
	}

	// Update is called once per frame
	void Update()
	{
		r.velocity = Vector3.back * moveSpeed;//自动下落速度
		Fire();
	}
	void Fire()
	{
		if ( Time.time > nextTime)
		{
			nextTime = spacetime + Time.time;//下一颗子弹运行的时间=每颗子弹飞行的时间+游戏运行时间
			// nAudioSource.PlayClipAtPoint(clip, transform.position, 1);
			Instantiate(bullet, pos.position, Quaternion.identity);
		}
	}
    void Move()
    {

        //设置边界值不让飞机飞出屏幕
        float xPos = Mathf.Clamp(transform.position.x, -6f, 6f);
        float zPos = Mathf.Clamp(transform.position.z, -43f, -22f);
        transform.position = new Vector3(xPos, transform.position.y, zPos);
    }
    //这也是用碰撞器来摧毁敌人和敌人子弹子弹
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
			enemyHp--; 
			if (enemyHp <= 0)
			{
				GameObject go = Instantiate(exlopion, transform.position, Quaternion.identity);
				//Destroy(go, 1f);
				Destroy(gameObject);
		    }
		}
        if (other.CompareTag("PlayerBullet"))
        {
			enemyHp--;
			Destroy(other.gameObject);
			if (enemyHp <= 0)
			{
				GameObject go = Instantiate(exlopion, transform.position, Quaternion.identity);
				//Destroy(go, 1f);
				Destroy(gameObject);
			}
		}
    }
    //用碰撞器来摧毁那些没有被飞机打到敌机
	void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Border"))
        {
			Destroy(gameObject);
        }
    }

这个敌人飞机就跟上面的陨石一样生成,这边就偷下懒,就不仔细写了

void Start(){

           StartCoroutine(Clone());

}

IEnumerator Clone()
    {
        while (true)
        {
            yield return new WaitForSeconds(8f);
            for (int i = 0; i < enemy.Length; i++)
            {
                int x = Random.Range(-6, 6);
                int z = Random.Range(-27, -14);
                Vector3 pos = new Vector3(x, 0.29f, z);
                Instantiate(enemy[i], pos, Quaternion.identity);
            }
            yield return new WaitForSeconds(8f);
        }



这里写的代码是敌人子弹的代码
private float speed = 5f;//这是定义的速度
	void Start () {
	
	}
	void Update () {
		transform.Translate(Vector3.back * Time.deltaTime * speed);//给了子弹位移
	}
	void OnTriggerExit(Collider other)
	{
		if (other.CompareTag("Border"))
		{
			Destroy(gameObject);
		}
	}
	void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
			GameManager1.gm1.playerHp--;
			Destroy(gameObject);//摧毁的是敌人的子弹
        }
    }
这是一个UI界面的代码,主要的作用是实现登录注册的功能
public InputField nameInput, pwdInput;
	public void RegButton()
    {
    //这是用本地存储来存储玩家输入的账号密码
        PlayerPrefs.SetString("name",nameInput.text);
        PlayerPrefs.SetString("pwd", pwdInput.text);
    }
    public void LoginButton()
    {
    //用GetString来读取存储在本地内存里面的数据
       string name= PlayerPrefs.GetString("name");
      string pwd=  PlayerPrefs.GetString("pwd");
      //符合这注册的账号密码就可以登录
        if (name==nameInput.text&&pwd==pwdInput.text)
        {
            SceneManager.LoadScene("Game");
            Time.timeScale = 1;
        }
    }

猜你喜欢

转载自blog.csdn.net/m0_48832826/article/details/128053132