项目03——《古罗马战场》

发布时间:2024年01月22日

接下来我们布置场景,我们的预期结果(功能分析)是:

? ? ? ? 实现两角色阵列面向冲锋

首先进入资源商店准备下载角色模型资源,

搜索Monster,

将免费资源导入unity包,

创建一个地面Plane,

对两个场景物体完全解压缩,

创建预制体包Prefabs,拖拽放回预制体包,

在场景中删除,

创建Scripts脚本包并创建脚本,

脚本命名为RandomInstance,

双击脚本打开代码编写:

using System.Collections;
using UnityEngine;
public class RandomInstance : MonoBehaviour{
? ? public GameObject monster1; ??//怪物1预制体
????public GameObject monster2; ??//怪物2预制体
????void Start(){
? ? ? ? if (monster1 == null)
? ? ? ? ? ? Debug.LogError("请将砖块预制体拽入参数");
? ? ? ? StartCoroutine(MonsterMatrix1());
? ? ? ? StartCoroutine(MonsterMatrix2());
? ? }
????//迭代器生成一组墙
????IEnumerator MonsterMatrix1(){
? ? ? ? for (int j = 0; j < 10; j++){
????????????//生成一行怪物1
????????????for (int i = 0; i < 10; i++){
? ? ? ? ? ? ? ? //(物体,位置,角度)
? ? ? ? ? ? ? ? Instantiate(monster1, new Vector3(i * 2f, 0f, 50 + j * 2f), Quaternion.AngleAxis(180f, Vector3.up)); ??
????????????????yield return new WaitForSeconds(1.3f); ??//产值
????????????}
? ? ? ? }
? ? }
? ? IEnumerator MonsterMatrix2(){
? ? ? ? for (int j = 0; j < 10; j++){
????????????//生成一行怪物2
????????????for (int i = 0; i < 10; i++){
? ? ? ? ? ? ? ? //(物体,位置,角度)
? ? ? ? ? ? ? ? Instantiate(monster2, new Vector3(i * 2f, 0f, j * 2f), Quaternion.identity);
? ? ? ? ? ? ? ? yield return new WaitForSeconds(0.7f); ??//产值
????????????}
? ? ? ? }
? ? }
}

ctrl + s 保存代码后将RandomInstance.cs脚本绑定在主摄像机MainCamera上,

并将怪物1与怪物2预制体拖拽至脚本框选中,

再创建脚本Speed.cs

编写Speed脚本:

using UnityEngine;
public class Speed : MonoBehaviour{
? ? public float moveSpeed;
? ? void Start(){
????????//初始化一个随机速度
????????moveSpeed = Random.Range(1f, 4f);
? ? }
????void Update(){
? ? ? ? transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
? ? }
}

将Speed绑定给Monster1物体,

将Speed绑定给Monster2物体,

调整编辑场景中的视角左键主摄像机 ctrl + shift + f 将运行视角与编辑视角同步,

运行即可实现,

?

End.

文章来源:https://blog.csdn.net/weixin_69360830/article/details/135756871
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。