【发布时间】:2018-09-20 23:15:04
【问题描述】:
我生成一个 Gameobjct 球体数组,并尝试对它们应用碰撞器以检测与我的角色控制器的碰撞。
我尝试了 serverel 方法,但没有任何效果。为什么 Unity 没有检测到碰撞?
生成数组的脚本:
public GameObject[] Chunkzufall(float l, float b, int n)
{
GameObject chunk = new GameObject();
GameObject[] chunks = new GameObject[n];
chunkSpeed = new float[n];
chunkMaxH = new float[n];
chunkMinH = new float[n];
for (int j = 0; j < n; j++)
{
float h = 1;
float posX = Random.Range(0.0f, b);
float posZ = Random.Range(0.0f, l);
GameObject group = Chunk(h);
group.transform.Translate(posX, 0.0f, posZ);
group.transform.parent = chunk.transform;
chunk.tag = "reset";
chunk.name = "chunk";
chunkSpeed[j] = (float) Random.Range(-0.04f, 0.04f);
chunkMaxH[j] = (float) Random.Range(2.0f, 10.0f);
chunkMinH[j] = (float) Random.Range(-2.0f, -10.0f);
chunk.AddComponent<SphereCollider>();
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;
chunks[j] = chunk;
}
return chunks;
}
public void MoveChunks(GameObject[] chunks)
{
int i = 0;
foreach(GameObject chunk in chunks)
{
Vector3 position = chunk.transform.GetChild(i).position;
if (position.y >= chunkMaxH[i] || position.y <= chunkMinH[i])
{
chunkSpeed[i] = chunkSpeed[i] * -1;
}
position.y = position.y - chunkSpeed[i];
chunk.transform.GetChild(i).position = position;
chunk.GetComponent<SphereCollider>().center = position;
i++;
}
i = 0;
}
碰撞触发功能:
private void OnTriggerEnter(Collider col) {
if(col.gameObject.tag == "reset") {
transform.position = new Vector3(startX, startY, startZ);
Debug.Log("chunk");
}
}
【问题讨论】:
-
嗨@Lazy Male 我相信this post 通过更新transform 解释了您所面临的问题以及“传送”对象的注意事项。希望设置 velocity 有帮助:)
-
您应该真正构建一个预制件并为您节省一些代码......但无论如何。也将刚体添加到您的
chunks,我假设您的播放器连接了刚体?这应该可以解决碰撞问题。如果不是,我真的看不到任何孩子附加到块:* Vector3 position = chunk.transform.GetChild(i).position;*
标签: c# visual-studio unity3d game-engine game-physics