Per aspera ad astra.循此苦旅,以达|

flyall

园龄:4年2个月粉丝:10关注:8

2023-03-03 22:30阅读: 336评论: 0推荐: 0

unity3D控制怪物的随机生成02

对target创建脚本

image

逻辑梳理

控制目标显示与隐藏

image

控制被射击

image

是否显示死亡动画

image

创建怪物数组

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

public class TargetManager : MonoBehaviour
{
    //1.获取我们设置的四种怪物:控制怪物的生成或销毁(显示或隐藏) 最开始是都不显示
    //建立数组
    public GameObject[] monsters;
}

保存后返回unity

image

将对象放入数组

image

随机生成怪物

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

public class TargetManager : MonoBehaviour
{
    //1.获取我们设置的四种怪物:控制怪物的生成或销毁(显示或隐藏) 最开始是都不显示
    //2.建立数组
    public GameObject[] monsters;
    //6.获得激活状态的怪物
    public GameObject activeMonster = null;

    //9.调用测试
    private void Start()
    {
        //10.遍历初始化目标怪的状态以及boxcollider状态
        foreach (GameObject monster in monsters)
        {
            monster.GetComponent<BoxCollider>().enabled = false;
            monster.SetActive(false);
        }
        ActiveMonster();
    }
    //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;
    }
}

本文作者:flyall

本文链接:https://www.cnblogs.com/flyall/p/17177246.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   flyall  阅读(336)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.