unity建房子脚本
时间: 2023-06-29 17:17:14 浏览: 245
以下是一个简单的 Unity 建房子脚本示例,用于创建一个基本的房屋模型:
```csharp
using UnityEngine;
public class HouseBuilder : MonoBehaviour
{
public GameObject wallPrefab; // 墙体预制件
public GameObject floorPrefab; // 地面预制件
public GameObject ceilingPrefab; // 天花板预制件
public float wallHeight = 3.0f; // 墙体高度
public float roomWidth = 10.0f; // 房间宽度
public float roomDepth = 10.0f; // 房间深度
void Start()
{
BuildHouse();
}
void BuildHouse()
{
// 创建墙体
GameObject leftWall = Instantiate(wallPrefab, transform.position, Quaternion.identity);
leftWall.transform.localScale = new Vector3(1, wallHeight, roomDepth);
leftWall.transform.position += new Vector3(-roomWidth / 2, wallHeight / 2, 0);
GameObject rightWall = Instantiate(wallPrefab, transform.position, Quaternion.identity);
rightWall.transform.localScale = new Vector3(1, wallHeight, roomDepth);
rightWall.transform.position += new Vector3(roomWidth / 2, wallHeight / 2, 0);
GameObject backWall = Instantiate(wallPrefab, transform.position, Quaternion.identity);
backWall.transform.localScale = new Vector3(roomWidth, wallHeight, 1);
backWall.transform.position += new Vector3(0, wallHeight / 2, -roomDepth / 2);
GameObject frontWall = Instantiate(wallPrefab, transform.position, Quaternion.identity);
frontWall.transform.localScale = new Vector3(roomWidth, wallHeight, 1);
frontWall.transform.position += new Vector3(0, wallHeight / 2, roomDepth / 2);
// 创建地面
GameObject floor = Instantiate(floorPrefab, transform.position, Quaternion.identity);
floor.transform.localScale = new Vector3(roomWidth, 1, roomDepth);
floor.transform.position += new Vector3(0, -0.5f, 0);
// 创建天花板
GameObject ceiling = Instantiate(ceilingPrefab, transform.position, Quaternion.identity);
ceiling.transform.localScale = new Vector3(roomWidth, 1, roomDepth);
ceiling.transform.position += new Vector3(0, wallHeight - 0.5f, 0);
}
}
```
这个脚本可以创建一个简单的房屋模型,包括四面墙体、地面和天花板。你可以将该脚本附加到一个空的 GameObject 上,并在 Inspector 窗口中设置墙体、地面和天花板的预制件,以及房间宽度、深度和墙体高度等属性。
需要注意的是,这只是一个简单的示例脚本,你可以根据自己的需求进行修改和扩展。如果你想要创建更复杂的房屋模型,需要使用更高级的建模工具和脚本来实现。
阅读全文