- Home /
Force a script generated primitive to test for collision
Hello,
I have a primitive sphere with a rigidbody and a script that creates a primitive square and adds a rigidbody to the created square. The square is generated already colliding with the sphere and both primitives have there rigidbodyconstraints set to freeze all so they will move move on collision.
How do I force the objects to test if they are colliding when the square is finished being generated?
If it helps I am using the following line to create the primitive:
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
I am using the following commands to create the rigid body for the new object:
Rigidbody gRigid = obj.AddComponent<Rigidbody>();
gRigid.constraints = RigidbodyConstraints.FreezeAll;
and the code to position the created primitive:
Bounds bounds = renderer.bounds
obj.transform.localPosition = new Vector3(bounds.center.x , bounds.center.y,
bounds.center.z);
This code is called inside the sphere.
Answer by codeguyross1 · Dec 07, 2012 at 09:42 PM
I figured out a way to do this using instantiate instead of the way I was creating the primitive before. The command is
Instantiate (spawnedCube, new Vector3(obj.transform.position.x, obj.transform.position.y, obj.transform.position.z), Quaternion.identity);
obj is the parent object. In this case, my sphere.
Hope this helps someone when testing collisions out in unity!
Answer by skalev · Dec 07, 2012 at 04:33 PM
if you have everything set to freeze, they won't move. The physics engine will do the collision checks for you as soon as the object is created.
I would think this also but it doesn't seem to do the check unless one of the objects move. I can use the gizmo in the scene view to move the object. Any idea how I would check collision between the two objects without moving either of them?
gRigid.constraints = RigidbodyConstraints.FreezeAll;
First of all this line means it won't move any how, you should comment that out.
If after you correct that, they still don't collide, then my guess is that collisions will only be calculated if one of the objects move. otherwise it makes no sense in a physics world.
I'm not sure what you are trying to achieve, if you explain the broader picture it might be more clear.
Your answer
Follow this Question
Related Questions
Objects with colliders going through walls and each other. 3 Answers
Prevent a specific RB to influence another, but still collide with everything 0 Answers
The object of type 'Rigidbody' has been destroyed but you are still trying to access it. 2 Answers
Rigidbody collision is delayed[FIXED] 0 Answers