unity3D设置Target位置和怪物类型18

设置每一组target怪物的编号

TargetManager

  //18.设置target编号
    public int targetPosition;

设置每一个怪物的编号

MonsterManager

//13.设置怪物的编号
    public int monsterType;

赋值

四个怪物
image

设置

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

public class TargetManager : MonoBehaviour
{
    //16.方便其他文件调用
    public static TargetManager _instance;
    //1.获取我们设置的四种怪物:控制怪物的生成或销毁(显示或隐藏) 最开始是都不显示
    //2.建立数组 保存所有该目标下的怪物
    public GameObject[] monsters;
    //6.获得激活状态的怪物 保存激活状态的怪物
    public GameObject activeMonster = null;

    //18.设置target编号 目标所在位置(0-8)
    public int targetPosition;

    //17.赋值
    private void Awake()
    {
        _instance = this;
    }

    //9.调用测试
    private void Start()
    {
        //10.遍历初始化目标怪的状态以及boxcollider状态
        foreach (GameObject monster in monsters)
        {
            monster.GetComponent<BoxCollider>().enabled = false;
            monster.SetActive(false);
        }
        //ActiveMonster();
        //12.调用迭代器方法:先注释上一句的直接调用
        StartCoroutine("AliveTimer");

    }

    //3.是否激活各种状态函数
    private void ActiveMonster()
    {
        //4.随机激活:得到index
        int index = Random.Range(0, monsters.Length);

        //5.激活怪物:需要先获得激活状态的怪物
        //赋值
        activeMonster = monsters[index];
        //7.激活
        activeMonster.SetActive(true);
        //8.激活box collider
        activeMonster.GetComponent<BoxCollider>().enabled = true;
        //14.调用
        StartCoroutine("DeathTimer");
    }
    //11.协程控制迭代器 设置怪物生成的等待时间
    IEnumerator AliveTimer()
    {
        yield return new WaitForSeconds(Random.Range(1, 5));
        ActiveMonster();

    }

    //14.控制死亡迭代器 使激活状态的怪变为未激活状态
    private void DeActiveMonster()
    {
        if (activeMonster != null)
        {

            activeMonster.GetComponent<BoxCollider>().enabled = false;
            activeMonster.SetActive(false);
            activeMonster = null;
        }
        //14.再次激活
        StartCoroutine("AliveTimer");
    }
    //设置死亡时间
    IEnumerator DeathTimer()
    {
        yield return new WaitForSeconds(Random.Range(3, 8));
        DeActiveMonster();
    }

    //15.更新怪物 在其他脚本中调用此方法
    public void UpdateMonsters()
    {
        //关闭所有协程
        StopAllCoroutines();
        if (activeMonster != null)
        {
            activeMonster.GetComponent<BoxCollider>().enabled = false;
            activeMonster.SetActive(false);
            activeMonster = null;
        }
        StartCoroutine("AliveTimer");
    }
    //19.随机
    public void ActivateMonsterByType(int type)
    {
        StopAllCoroutines();
        if (activeMonster != null)
        {
            activeMonster.GetComponent<BoxCollider>().enabled = false;
            activeMonster.SetActive(false);
            activeMonster = null;
        }
        activeMonster = monsters[type];
        activeMonster.SetActive(true);
        activeMonster.GetComponent<BoxCollider>().enabled = true;
        StartCoroutine("DeathTimer");
    }
}

解除动物预制体关系

image

复制后记得编号

image

放入数组

image

解决小bug

MonsterManager
image

image

换动物皮肤

image

posted @ 2023-03-05 21:54  flyall  阅读(284)  评论(0)    收藏  举报