iT邦幫忙

2021 iThome 鐵人賽

DAY 19
0
Mobile Development

30天輕鬆學會unity自製遊戲系列 第 19

30天輕鬆學會unity自製遊戲-製作BOSS

先把BOSS放在遊戲場景的最後面,跟Player一樣把3個Boss0~2放到場景上,存一個Boss待機動畫,調整一下圖層
https://ithelp.ithome.com.tw/upload/images/20210919/20140598mZDYAJcZyQ.png
跟製作敵機一樣把受傷動畫製作出來,Create New Clip創造出一個Boss的受傷動畫,把Boss0~2~Boss Hit都放進動畫Animation
https://ithelp.ithome.com.tw/upload/images/20210919/20140598KhwlfdtXkb.png
看你一次受傷要閃爍幾次(複製貼上三份),記得最後把Loop Time打勾取消
https://ithelp.ithome.com.tw/upload/images/20210919/20140598mUPpbCvZBY.png
跟敵機一樣把hit裝上動畫控制器,給一個trigger(名稱等等程式要用記得要一模一樣…)
https://ithelp.ithome.com.tw/upload/images/20210919/20140598Zra757pnrr.png
接下來就來寫一下Boss(BossAI)程式,可以拿敵機(EnemyAI)的程式來參考,滑鼠按住程式的名稱視窗,可以調整成雙邊程式
https://ithelp.ithome.com.tw/upload/images/20210919/20140598vwgYjU8LlT.png
可以開始養自己的程式,之後要用都可以拿來參考,慢慢就越來越多方法可以解決專案
https://ithelp.ithome.com.tw/upload/images/20210919/20140598jpCZd6xWDE.png
把敵機先都複製到BossAI(會寫程式的也可以用其他方法…),一樣加裝鋼體跟碰撞器Collider(可以把is Trigger點開)…Boss來簡單改一下攻擊方式.
https://ithelp.ithome.com.tw/upload/images/20210919/20140598jszuT6J0Ka.png
開BossAI跟敵機一樣的程式我就不多做解釋囉~

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossAI : MonoBehaviour
{
    private Animator anim;
    [SerializeField] float speed = 1f;
    bool right;
    [SerializeField] float hp = 100f;
    [SerializeField] GameObject enemyBullet = null;
	//多創造一個物件unity放上小兵
    [SerializeField] GameObject enemy = null;

    void Start()
    {
        right = true;
        anim = GetComponent<Animator>();
        InvokeRepeating("Attack", 1f, 2f);
    }

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

        if (!right)
        {
            transform.Translate(speed * -1 * Time.deltaTime, 0, 0);
            if (gameObject.transform.position.x <= 0.8)
                right = true;
        }
        if (right)
        {
            transform.Translate(speed * 1 * Time.deltaTime, 0, 0);
            if (gameObject.transform.position.x >= 5.2)
                right = false;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.tag == "PlayerBullet")
        {
            hp -= 5f;
            anim.SetTrigger("hit");
            if (hp <= 0)
                Destroy(gameObject);
        }
    }
    void Attack()
    {
		//(40%)創造的數字在40下執行
        if(GetRandom(40))
		//創造一個子彈
        Instantiate(enemyBullet, transform.position, Quaternion.identity);
//(30%)否則創造的數字在70下執行(所以40~70才會執行)
else if(GetRandom(70))
        {
			//(迴圈五次)(設定一個整數i=0,i小於5時,i會加一)
            for (int i = 0; i < 5; i++)
            {	
//設定一個暫時的三維向量tmep=新的三維向量(x軸+I,y軸不變,Z軸不變)
Vector3 temp = new Vector3(transform.position.x + i, transform.position.y, transform.position.z);
//創造(子彈,剛剛設定暫時的三維位置tmep,角度不變)
                Instantiate(enemyBullet, temp, Quaternion.identity);
            }//這裡發出來的子彈就一次五顆
        }
        else if(GetRandom(100)) (30%)否則創造的數字在100下執行(所以
		70~100才會執行)
		//創造(敵機(小兵),位置不變,角度不變)
        Instantiate(enemy, transform.position, Quaternion.identity);

    }
	//取一個布林值名叫GetRandom (要放一個整數p)
    bool GetRandom(int p)
    {
		//設定一個整數o =隨機0~100
        int o = Random.Range(0, 100);
		如果(隨機的數字小於設定的數字顯示true)
        if (o < p)
        {
            return true;
        }
        Else(隨機的數字大於設定的數字顯示false)
        {
            return false;
        }
    }
}

最後把敵機(小兵),拖曳到預製物,最後在放入Boss的物件空值裡
https://ithelp.ithome.com.tw/upload/images/20210919/20140598FOhOAbZ4NL.png


上一篇
30天輕鬆學會unity自製遊戲-設定畫面按鈕
下一篇
30天輕鬆學會unity自製遊戲-調整攝影機
系列文
30天輕鬆學會unity自製遊戲30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言