Unity 实用技巧 之 简便的物体一波一波自动生成

 时间:2024-10-12 04:50:18

Unity 实用技巧 之 简单的物体一波一波自动生成。在塔防游戏中,需要敌人一波一波生成,本节介绍一波一波物体随机生成的简单案例,具体如下

Unity 实用技巧 之 简便的物体一波一波自动生成

工具/原料

Unity

Instantiate

一、知识要点

1、Object.Instantiate:1)功能描述Clones the objectoriginaland returns the clo荏鱿胫协ne.This function makes a copy of an object in a similar way to the Duplicate command in the editor. If you are cloning aGameObjectthen you can also optionally specify its position and rotation (these default to the original GameObject's position and rotation otherwise). If you are cloning aComponentthen the GameObject it is attached to will also be cloned, again with an optional position and rotation.When you clone aGameObjectorComponent, all child objects and components will also be cloned with their properties set like those of the original object.2)使用案例using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour { public Rigidbody projectile; void Update() { if (Input.GetButtonDown("Fire1")) { Rigidbody clone; clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; clone.velocity = transform.TransformDirection(Vector3.forward * 10); } }}

2、方法要点:1)设置生成单体的计时器和时间间隔 pr坡纠课柩ivatefloattimerOne=1f;privateflo锾攒揉敫attimeOne=1.0f;2)设置生成一波的计时器和时间间隔 privatefloattimerWave=0f;privatefloattimeWave=10.0f;3)设置生成一波的数量和预制体 privateintcountPerWave=0;publicGameObjectspawnPerfab;4)计时的方法 timerWave+=Time.deltaTime;

二、实用技巧 之 简便的物体一波一波自动生成

1、打开Unity,新建一个空工程,具体如下

Unity 实用技巧 之 简便的物体一波一波自动生成

2、在场景中,新建一个“Plane”,和“Cube”,调小“Cube”的比例,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

3、新建2个脚本,命名为“MoveDestroyTest”、“SpawnTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

4、在打开的“MoveDestroyTest”脚本上编写代码,首先在Start函数里面调用3庙后自我销毁函数,然后在Update函数里面设置自我移动,最后实现自我销毁函数,代码及代码说明如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

5、“MoveDestroyTest”脚本具体内容如下usingUnityEngine;publicclassM泠贾高框oveDestroyTest:MonoBehaviour{//UsethisforinitializationvoidStart(){DeatroySlef3Second();}//UpdateiscalledonceperframevoidUpdate(){transform.Translate(Vector3.right*Time.deltaTime*2);}privatevoidDeatroySlef3Second(){Destroy(transform.gameObject,3);}}

6、在打开的“SpawnTest”脚本上编写代码,首先设置生成一个预制体的计时器和时间间隔,以及一波预制体生成的计时器和时间间隔,然后设置生成的数量计数和生成预制体,接着先计时一波的时间,在时间内以及数量内进行每一个的计时与生成计数,随后一波时间到达后,重新计时与重置生成预制体数量,代码及代码说明如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

7、“SpawnTest”脚本短铘辔嗟具体内容如下:usingUnityEngine;publicclassSpawnTest:MonoBehaviour{privatefloattimerOne=1f;privatefloattimeOne=1.0f;privatefloattimerWave=0f;privatefloattimeWave=10.0f;privateintcountPerWave=0;publicGameObjectspawnPerfab;//UpdateiscalledonceperframevoidUpdate(){timerWave+=Time.deltaTime;if(timerWave<timeWave&&countPerWave!=5){timerOne+=Time.deltaTime;if(timerOne>timeOne){Instantiate(spawnPerfab,newVector3(-3.5f,0.5f,Random.Range(-4.0f,4.0f)),spawnPerfab.transform.rotation);countPerWave++;timerOne-=timeOne;}}if(timerWave>=timeWave){timerWave-=timeWave;countPerWave=0;}}}

8、脚本编译正确后,回到 Unity 界面,把“MoveDestroyTest”赋给“Cube”,并把“Cube”做成预制体,场景中的“Cube”,可以删除掉,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

9、在场景中新建一个“GameObject”,把脚本“SpawnTest”赋给“GameObject”,并把预制体“Cube”赋值给脚本“SpawnTest”,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

10、运行场景,即可看到场景中预制体一波一波的自动生成,自动移动,自动销毁,具体如下图

Unity 实用技巧 之 简便的物体一波一波自动生成

11、到此,《Unity 实用技巧 之 简便的物体一波一波自动生成》讲解结束,谢谢

  • unity怎么打开资源商店
  • U3D如何解决物体穿透的问题
  • 如何在Unity3D5.x中添加复杂(专业版)的水效果?
  • Unity怎么新建预制体
  • Unity如何在场景中添加场景
  • 热门搜索
    国庆手抄报图片大全 感恩节手抄报内容 科技手抄报图片 圣诞手抄报 国庆手抄报简单又漂亮 六年级语文手抄报 父亲节手抄报好画字少 感恩母亲的手抄报图片 儿童中秋节手抄报 关于抗击疫情的手抄报