【发布时间】:2022-06-13 22:19:48
【问题描述】:
我正在学习一门课程,但我被困在一项任务中。
任务是:
更改创建迷宫的脚本,使迷宫的长度和宽度不再在脚本中指定,而是可以在外部使用变量进行设置。为了降低工作量,您可以将值限制为偶数值——例如,2、4 或 20。确保脚本只能使用有效的经度和纬度值。如果值无效,脚本应该以一条消息终止。 该脚本应自动将地板缩放到适合迷宫的大小并生成外墙和内墙。您必须根据地板的大小来定位外墙的第一块石头和内墙部分的第一块石头。
您可以将示例中的游戏对象用于地板和墙壁部分。您可以在固定位置设置迷宫的入口和出口。 您可以通过从上方查看场景视图中创建的迷宫来测试您的脚本是否正常工作。请记住,您还需要更改用于创建内墙件的循环。通过的次数必须自动从迷宫的长度和宽度推导出来。 相应更改的脚本如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Labyrinth : MonoBehaviour
{
public Transform wallItem;
public int countX;
public int countZ;
// Use this for initialization
void Start ()
{
float sizeX;
Vector3 position;
if (wallItem == null)
{
Debug.Log ("Es wurde kein Objekt übergeben");
return;
}
float newPosX = wallItem.position.x;
float newPosZ = wallItem.position.z;
Transform myChild = wallItem.transform.GetChild (0);
if (myChild == null)
{
Debug.Log ("Es wurde kein untergeordnetes Objekt gefunden");
return;
}
MeshFilter meshFilter = myChild.GetComponent<MeshFilter> ();
if (meshFilter == null)
{
Debug.Log ("Es wird ein Objekt mit Mesh benötigt");
return;
}
Mesh mesh = meshFilter.mesh;
Vector3 meshSize = mesh.bounds.size;
Vector3 scale = myChild.transform.localScale;
sizeX = meshSize.x * (scale.x);
for (int runX = 0; runX < countX; runX++)
{
for (int runZ = 0; runZ < countZ; runZ++)
{
newPosX = wallItem.position.x + sizeX * runX;
newPosZ = wallItem.position.z + sizeX * runZ;
position = new Vector3 (newPosX, wallItem.position.y, newPosZ);
Instantiate(wallItem, position, Quaternion.Euler(0, Random.Range(0, 4) * 90, 0));
}
}
}
// Update is called once per frame
void Update ()
{
}
}
【问题讨论】:
-
这段代码是C#?
标签: visual-studio unityscript maze