如果問題太難了,可以逐步解決。我想,你需要位置的字段和改造公共
實例化的東西在給定的位置:
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour
{
public Vector3 Position;
[SerializeField]
GameObject ThingToSpawn;
void Start()
{
if (ThingToSpawn == null)
{
Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name);
return;
}
Instantiate (ThingToSpawn,Position,Quaternion.identity);
}
}
實例化的東西在隨機位置之一:
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour
{
public Vector3[] Positions;
[SerializeField]
GameObject ThingToSpawn;
void Start()
{
if (ThingToSpawn == null)
{
Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name);
return;
}
Instantiate (ThingToSpawn,Positions[Random.Range(0,3)],Quaternion.identity);
}
}
隨機選擇三個位置和實例隨機一個東西。我不確定擁有一個公開的轉換列表是一個很好的解決方案,但是Unity對於構造函數並不適用。 (如果你只在檢查assing它,使它私人和使用[SerializeField]
)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
public class Spawn : MonoBehaviour
{
public ReadOnlyCollection<Vector3> Positions;
public List<Transform> Transforms=new List<Transform>();
[SerializeField]
GameObject ThingToSpawn;
void Start()
{
if (ThingToSpawn == null)
{
Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name);
return;
}
if (Transforms == null)
{
Debug.LogError ("Transforms variable is null for instance of Spawn.cs at "+transform.name);
return;
}
if (Transforms.Count == 0)
{
Debug.LogError ("No transforms provided for instance of Spawn.cs at "+transform.name);
return;
}
var positionsList =
Enumerable.Range (0, 3)
.Select (i => Transforms [Random.Range (0, Transforms.Count)].position)
.ToList();
Positions = new ReadOnlyCollection<Vector3> (positionsList);
Instantiate (ThingToSpawn,Positions[0],Quaternion.identity);
}
}
選擇三個位置隨機。在其中一個實例化任意東西。檢查哪些是正確的,哪些是中間的,哪些是剩餘的,但不關心重疊。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Spawn : MonoBehaviour
{
public List<Transform> Transforms=new List<Transform>();
public Vector3 LeftPosition{ get; private set;}
public Vector3 MiddlePosition{ get; private set;}
public Vector3 RightPosition{ get; private set;}
[SerializeField]
GameObject ThingToSpawn;
void Start()
{
if (ThingToSpawn == null)
{
Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name);
return;
}
if (Transforms == null)
{
Debug.LogError ("Transforms variable is null for instance of Spawn.cs at "+transform.name);
return;
}
if (Transforms.Count == 0)
{
Debug.LogError ("No transforms provided for instance of Spawn.cs at "+transform.name);
return;
}
var Positions =
Enumerable.Range (0, 3)
.Select (i => Transforms [Random.Range(0,Transforms.Count)].position)
.OrderBy (i => i.x)
.ToList();
LeftPosition = Positions [0];
MiddlePosition = Positions [1];
RightPosition = Positions [2];
Instantiate (ThingToSpawn,new[]{LeftPosition,MiddlePosition,RightPosition}[Random.Range(0,3)],Quaternion.identity);
}
}
和上面一樣,但是要注意重疊。分爲兩個文件
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Spawn : MonoBehaviour
{
public List<Transform> Transforms=new List<Transform>();
public Vector3 LeftPosition{ get; private set;}
public Vector3 MiddlePosition{ get; private set;}
public Vector3 RightPosition{ get; private set;}
[SerializeField]
GameObject ThingToSpawn;
void Start()
{
if (ThingToSpawn == null)
{
Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name);
return;
}
if (Transforms == null)
{
Debug.LogError ("Transforms variable is null for instance of Spawn.cs at "+transform.name);
return;
}
if (Transforms.Count <= 3)
{
Debug.LogError ("Less than 3 transforms provided for instance of Spawn.cs at "+transform.name);
return;
}
var Positions =
Enumerable.Range (0, 3)
.RandomShuffle()
.Take(3)
.Select (i => Transforms [i].position)
.OrderBy (i => i.x)
.ToList();
LeftPosition = Positions [0];
MiddlePosition = Positions [1];
RightPosition = Positions [2];
Instantiate (ThingToSpawn,new[]{LeftPosition,MiddlePosition,RightPosition}[Random.Range(0,3)],Quaternion.identity);
}
}
在RNGUtils.cs
。使用Fisher-Yates算法
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class RNGUtils
{
static void SwapListElements<T>(IList<T> list, int firstIndex, int secondIndex)
{
T tmp = list[firstIndex];
list[firstIndex]=list[secondIndex];
list[secondIndex]=tmp;
}
public static IList<T> RandomShuffle<T>(this IEnumerable<T> enumerable)
{
List<T> res = enumerable.ToList();
for (int i = 0; i < res.Count; ++i)
{
SwapListElements (res,i,Random.Range(i,res.Count));
}
return res;
}
}
正如你可以看到,上述兩種使用語法new[]{a,b,c}[]
最後的解決方案。 It is valid.您提到的錯誤很可能是編譯器錯誤。他們通常可以通過搜索他們的名字和錯誤代碼來逐個修復。
在您的代碼中,您將int
指定爲Transform
,並發現了一些可以通過查看編譯器錯誤來解決的其他錯誤。我建議你從像spoj/project euler這樣的網站解決幾十個最簡單的任務,不要犯基本錯誤。我每次學習新的編程語言時都會使用它們
希望這有助於!
你的意思是'Instantiate(new [] {leftPosition,middlePosition,rightPosition} [Random.Range(0,3)])'? – user2136963
你用'位置=新的Vector2 [3];'初始化vector2';'但是你在使用它之前從未給每個位置指定值'leftPosition = positions [Random.Range(0,positions.Length)];'你期望什麼將這3個值分配給位置[0],位置[1]和位置[2] ?. – Programmer
他們是公共vector2列在頂部,並且團結要求我添加數字,所以我認爲3代表我想要它考慮的3個職位。那麼猜錯了嗎? – Simcha