我试图在我的我的Minecraft克隆中创建一个脚本,但它不断返回此错误:
Assets/Materials/minecraft_assets/WorldGeneration.cs(79,41): error CS0029: Cannot implicitly convert type UnityEngine.GameObject to UnityEngine.Vector3
这是我脚本中的所有代码(我知道代码很乱):
using UnityEngine;
using System.Collections;
public class Block
{
public int type;
public bool vis;
public Block(int t, bool v)
{
type = t;
vis = v;
}
}
public class WorldGeneration : MonoBehaviour {
public static int width = 128;
public static int depth = 128;
public static int height = 128;
public int heightScale = 20;
public float detailScale = 25.0f;
public GameObject grassBlock;
public GameObject sandBlock;
public GameObject snowBlock;
Block[,,] worldBlocks = new Block[width, height, depth];
// Use this for initialization
void Start ()
{
int seed = (int) Network.time * 10;
for (int z = 0; z <= depth; z++) {
for (int x = 0; x <= width; x++) {
int y = (int) (Mathf.PerlinNoise((x+seed)/detailScale, (z+seed)/detailScale) * heightScale);
Vector3 blockPos = new Vector3 (x, y, z);
CreateBlock (y, blockPos, true);
while (y > 0) {
y--;
blockPos = new Vector3 (x, y, z);
CreateBlock(y, blockPos, false);
}
}
}
}
// Use this for creating blocks
void CreateBlock(int y, Vector3 blockPos, bool create)
{
if (y > 15) {
if (create)
Instantiate (snowBlock, blockPos, Quaternion.identity);
worldBlocks [(int)blockPos.x, (int)blockPos.y,(int) (blockPos.z)] = new Block (1, create);
}
else if (y > 5) {
if (create)
Instantiate (grassBlock, blockPos, Quaternion.identity);
worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (2, create);
}
else {
if (create)
Instantiate (sandBlock, blockPos, Quaternion.identity);
worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (3, create);
}
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2.0f, Screen.height / 2.0f, 0));
if (Physics.Raycast (ray, out hit, 1000.0f)) {
Vector3 blockPos = hit.transform.gameObject.;
//This is the bottom block. Don't delete it
if((int)blockPos.y == 0) return;
worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = null;
Destroy (hit.transform.gameObject);
for (int x = -1; x <= 1; x++)
for (int y = -1; y <= 1; y++)
for (int z = -1; z <= 1; z++) {
if (!(x == 0 && y == 0 && z == 0)) {
Vector3 neighbor = new Vector3 (blockPos.x + x, blockPos.y + y, blockPos.z + z);
DrawBlock (neighbor);
}
}
}
}
// Use this for returing values
void DrawBlock(Vector3 blockPos)
{
if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] == null)
return;
if (!worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis) {
worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = true;
if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 1)
Instantiate (snowBlock, blockPos, Quaternion.identity);
} else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 2)
Instantiate (grassBlock, blockPos, Quaternion.identity);
else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 3)
Instantiate (sandBlock, blockPos, Quaternion.identity);
else
worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = false;
}
/* End Of Script*/
我尝试通过将blockPos更改为类似这样的游戏对象来调试它:
Vector3 blockPos = (gameObject) hit.transform.gameObject;
但它返回了1000条错误消息!
有任何想法可以帮助任何人吗?
我在HP Pavilion TouchSmart 14 Sleekbook和MonoDevelop 2.0上使用Unity 5.4.2f2。
答案 0 :(得分:1)
根据Unity3D文档,GameObject
继承自System.Object
。此外,根据文档,GameObject
或Vector3
上没有隐式强制转换运算符重载,将此问题留给一个简单的答案:
GameObject
不是Vector3
,不能明确表示为一个。
要解决您的问题,请参阅Fredrik的答案。他似乎对Unity3D有所了解,而不仅仅是像我这样的.NET;)。
答案 1 :(得分:1)
就像例外情况一样,您的代码中存在错误。您正在尝试将VectorObject保存在Vector3中。
第79行:
Vector3 blockPos = hit.transform.gameObject.;
这不是正常工作的代码。你想要:
Vector3 blockPos = hit.transform.position;