Reputation: 41
I generate an Gameobjct array of speheres and try to apply a collider to them to detect collision with my character controller.
i tried serverel methods, nothing worked. Why Unity doent detect the collision ?
Script for generating the array :
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;
}
collision trigger function :
private void OnTriggerEnter(Collider col) {
if(col.gameObject.tag == "reset") {
transform.position = new Vector3(startX, startY, startZ);
Debug.Log("chunk");
}
}
Upvotes: 4
Views: 112
Reputation: 992
You could add a RigidBody
to all of the spheres but that would be very inefficient if you want to have many of them. It is enough to have 1 RigidBody
for every 2 objects that you want to check collision against.
So in your case it would be enough to put a RigidBody
component on your character controller object.
Also, make sure the RigidBody
is not set to Kinematic.
Upvotes: 1
Reputation: 733
Do you have Rigidbody
attached? The objects need Rigidbody
attached to it to be able to detect collision. And do note that for each pair of collider and collided objects, having one of them with Rigidbody
attached is enough to capture the event. In your case, if you don't want to process the collision betweens spheres, then attach Rigidbody
to your character object is good enough.
Upvotes: 1
Reputation: 125245
To detect a trigger on the dynaimcally created object, you have to enable the IsTrigger
flag, add collider to the object. The object must also have Rigidbody
attached to it. It looks like you already have the IsTrigger
flag and a collider but you are missing Rigidbody
.
Replace this:
chunk.AddComponent<SphereCollider>();
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;
with
chunk.AddComponent<SphereCollider>();
chunk.AddComponent<Rigidbody>(); //ADDS RIGIDBODY COMPONENT
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;
Upvotes: 3