- Home /
Collsions - Objects just passing through each other
I'm halfway through my Game Production but i'm having trouble with collisions, each object (that I want to collide) has a rigidbody, isKinematic turned off, and a relative force added plus a physics material added to the mesh collider, but whenever I run the program and set it so that a certain amount of objects collide with each other, they just pass straight through each other as if the colliders weren't there.
However my bullet script works fine when detecting that it has hit something, but I thought the Unity Engine was supposed to be able to handle this kind of stuff with world collisons without the need for extensive coding.
Here is my script so far (for the objects I want to collide)
pragma strict
var object : GameObject;
function Start()
{
for (var i : int = 0; i < 100; i++)
{
var AsteroidSpeed : Vector3 = Vector3 (0,0,1);
var position : Vector3 = Random.insideUnitSphere * 500;
transform.position.x = position.x;
transform.position.y = position.y;
transform.position.z = position.z;
var rotation = Quaternion.Euler ( Random.Range (0, 360), Random.Range (0, 360), Random.Range (0, 360) );
Instantiate (object, position, rotation);
rigidbody.isKinematic = false;
rigidbody.detectCollisions = true;
rigidbody.MovePosition ( rigidbody.position + AsteroidSpeed * Time.deltaTime);
yield WaitForSeconds (0.0001);
}
}
Any help would be much appreciated as I'm banging my head against the wall here
Have you set the collision mode to continuous dynamic?
Answer by aldonaletto · Jun 17, 2012 at 08:14 PM
What do you expect this code to do? It actually repeat 100 times a weird sequence: generate a random point inside a sphere of radius 500 centered at (0,0,0), move its owner object to this random point, rotate it randomly, instantiate a clone of object at this position and let it alone there, then move the owner object a little in the Z direction, wait for the next frame and repeat the sequence.
If you want to generate 100 asteroids at random positions and moving to random directions, use a script like this attached to some scene object (not the asteroids!):
var object: GameObject; // drag asteroid prefab here var speed: float = 10; // set the asteroid speed
function Start(){ for (var i : int = 0; i < 100; i++){ // create an asteroid at random position and direction: var position : Vector3 = Random.insideUnitSphere 500; var rotation = Random.rotation; var asteroid: GameObject = Instantiate (object, position, rotation); // set the asteroid velocity to its forward direction: asteroid.rigidbody.velocity = asteroid.transform.forward speed; } }
When you say scene object am i to assume that you mean an empty Gameobject? Also what about the original question where i'm having problems with the asteroids passing through each other rather then colliding with each other and moving off in a different direction as I want them to do that as well as bounce off a perimeter sphere, also thanks for the revised code, will test it after work :)
The script may be attached to an empty object, or the camera, or any other object - except the asteroids, because this would crash Unity (each new asteroid would generate 100 new ones, and each of them would generate another 100, and so on).
A common problem is the discrete nature of collision detection: when a rigidbody moves too fast, it can be before an obstacle in one physics cycle and after it in the next, what misses the collision.
As @whydoidoit suggested, you could try continuous collision detection, but for 100 rigidbodies this would kill your framerate.
The script worked perfectly thank you, on the subject of collisions i worked out the problem, i tried continous collision detection and that didn't work, i had to set the rigidbody to "convex" but since that only works with upto 255 triangles it wouldn't recognise a collision, using a primitive collider however solved the problem :) even with 100 asteroids :p