Unity3D效果實現:怪物的控制和怪物刷新點


前言: 

 在制作游戲的過程中我們會需要不同的敵人或者說是怪物,每個怪物都有自己的狀態,在一些RPG游戲中還會有固定的刷怪點,本文就來描述如何用腳本實現怪物的控制和怪物的刷新


效果:

  • 玩家靠近時怪物會開始攻擊玩家,到達一定距離之后攻擊玩家,如果玩家不在范圍內則會在出生點徘徊
  • 刷新點會一次產生三個怪

實現方法:

1.敵人刷新點

  • 我們需要建立一個空物體來作為刷新點,隨后將腳本組件添加給它
  • 將敵人預制體放入該腳本中即可 
    • 腳本代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPoint : MonoBehaviour
{
    public GameObject EnemyPre;
    // Start is called before the first frame update
    public int Num = 3;
    private float timer;
    // Update is called once per frame
    void Update()
    {
        //計時器時間增加
        timer += Time.deltaTime;
        //2s檢測一次
        if(timer>2)
        {
            //重置計時器
            timer = 0;
            //查看有幾個敵人
            int n = transform.childCount;
            if (n < Num)
            {
                Vector3 v = transform.position;
                v.x += Random.Range(-5, 5);
                v.z += Random.Range(-5, 5);
                //隨機確定一個旋轉
                Quaternion q = Quaternion.Euler(0, Random.Range(0, 360), 0);
                //創建一個敵人
                GameObject enemy = GameObject.Instantiate(EnemyPre, v, q);
                //設置父子關系
                enemy.transform.SetParent(transform);
            }
        }
    }
}

2.敵人

  • 敵人需要判斷和玩家之間的距離,隨后開始跟蹤玩家,如果超出范圍則回到刷新點,當和玩家小於一定距離時開始攻擊,如果玩家死亡則停止攻擊
  • 敵人死亡后掉落物品,
  • 腳本代碼添加給敵人,並給他的tag設置為“enemy”
  • 腳本代碼如下
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EnemyControl : MonoBehaviour
    {
        public int ID = 101;
        //主角
        private PlayerControl player;
        public int Hp = 100;
        //攻擊力
        public int Attack = 20;
        //出身點位置
        private Vector3 position;
        //動畫器組件
        private Animator animator;
        //攻擊計時器
        private float timer = 1;
        //當前是否攻擊
        private bool isAttack = false;
        //血瓶預制件
        public GameObject PotionPre;
        // Start is called before the first frame update
        void Start()
        {
            player = GameObject.FindWithTag("Player").GetComponent<PlayerControl>();
            //獲得出生點位置
            position = transform.position;
            //獲取動畫器組件
            animator = GetComponent<Animator>();
        }
    
        // Update is called once per frame
        void Update()
        {
            if(player.hp<=0||Hp<=0)
            {
                animator.SetBool("Run", false);
                animator.SetBool("Attack", false);
                return;
            }
            //獲得與主角的距離
            float distance = Vector3.Distance(player.transform.position, transform.position);
            Debug.Log(distance);
            //如果7m沒發現主角
            if(distance>7f)
            {
                if (Vector3.Distance(transform.position, position) > 1f)
                //轉向出生點
                {
                    transform.LookAt(new Vector3(position.x, transform.position.y, position.z));
                    //向前移動
                    transform.Translate(Vector3.forward * 2 * Time.deltaTime);
                    animator.SetBool("Run", true);
                }
                else
                {
                    //停止播放移動畫面
                    animator.SetBool("Run", false);
                }
            }
            else if(distance>3f)
            {
                //如果與主角的距離在3到7m之間,則朝主角移動
                //轉向玩家
               
                transform.LookAt(new Vector3(player.transform.position.x, transform.position.y,
                    player.transform.position.z));
                //向前移動,這里我們直接移動,也可用導航
                transform.Translate(Vector3.forward * 2 * Time.deltaTime);
                animator.SetBool("Run", true);
                //保證當前不在攻擊狀態
                isAttack = false;
            }
            else
            {
                //3m內停止移動,開始攻擊
                animator.SetBool("Run", false);
                //轉向玩家
                transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
                animator.SetBool("Attack", true);
                if(isAttack==false)
                {
                    isAttack = true;
                    timer = 1;
                }
            }
    
            timer += Time.deltaTime;
            if(timer>=2)
            {
                timer = 0;
                player.GetDamage(Attack);
            }
        }
    
        public void GetDamage(int damage)
        {
            if(Hp>0)
            {
                GetComponentInChildren<HpManager>().ShowText("-" + damage);
                Hp -= damage;
            }
            if(Hp<=0)
            {
                Instantiate(PotionPre, transform.position, transform.rotation);
                animator.SetTrigger("Die");
                //給任務系統報告,擊殺了一個ID為101的敵人
                QuestManager.Instance.AddEnemy(ID);
                Destroy(gameObject, 20);
            }
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM