我一直在統一制作生存游戲,并生成了具有以下功能的樹
生成樹函式
void GenerateTree(int x, int y)
{
//define our tree
//generate log
int treeHeight = Random.Range(minTreeHeight, maxTreeHeight);
for(int i = 0; i < treeHeight; i )
{
PlaceTile(log, x, y i);
}
//generate leaves
PlaceTile(leaf,x,y treeHeight);
PlaceTile(leaf, x, y treeHeight 1);
PlaceTile(leaf, x, y treeHeight 2);
PlaceTile(leaf, x-1, y treeHeight);
PlaceTile(leaf, x-1, y treeHeight 1);
PlaceTile(leaf, x 1, y treeHeight);
PlaceTile(leaf, x 1, y treeHeight 1);
}
PlaceTile 功能
public void PlaceTile(Sprite tileSprite, int x, int y)
{
GameObject newTile = new GameObject();
float chunkCoord = (Mathf.Round(x / chunkSize) * chunkSize);
chunkCoord /= chunkSize;
Debug.Log(chunkCoord);
newTile.transform.parent = worldChunks[(int)chunkCoord].transform;
newTile.AddComponent<SpriteRenderer>();
newTile.GetComponent<SpriteRenderer>().sprite = tileSprite;
newTile.name = tileSprite.name;
newTile.transform.position = new Vector2(x 0.5f, y 0.5f);
worldTiles.Add(newTile.transform.position - (Vector3.one * 0.5f));
}
我相信我需要使用if陳述句來檢查附近是否有另一個日志,但我需要有關如何執行此操作的幫助。
uj5u.com熱心網友回復:
Vector2.Distance(Vector2D,Vector2D)
https://docs.unity3d.com/ScriptReference/Vector2.Distance.html
if(Vector2.Distance(Log1Location,Log2Location) > 10)
{
//Spawn?!
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469048.html