- Home /
Help with Collision Detection for explosions?
Hey guys
I'm making a simple action where a spaceship needs to collide with an asteroid, and the code I'm using for it is here:
var explosionPrefab : Transform;
function OnCollisionEnter(collision : Collision) {
var contact : ContactPoint = collision.contacts[0];
var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
var pos : Vector3 = contact.point;
Instantiate (explosionPrefab, pos, rot);
Destroy (gameObject);
}
I personally can't see anything wrong with this, as I've set both the spaceship and the object with non-kinematic RigidBodies, and Unity doesn't warn me of any scripting mistakes, but whenever I try to crash into an asteroid, the ship goes right through it, with no explosion effect. Can someone please help me with this, as I need the project to be submitted later today?enter code here
Thanks
Harry
did you write to the debug.log if OnCollisionEnter is even executing? Do both objects have $$anonymous$$esh Colliders attached and active?
Thanks for the suggestion, but I just did and I got an error saying "The hull has more than 255 polygons. This is invalid" Any more ideas? Thanks :)
Just what, added a mesh collider? This code is directly from: http://docs.unity3d.com/Documentation/ScriptReference/Collision-contacts.html yes? is the oncollision enter function added to a script that is attached to the asteroid object (or whatever it is)? From what I understand, the object being hit needs a rigid body to detect contact points. Another thing, is I'm not sure why you are trying to get the contact point just to set an explosion. The explosion is likely to make the precision a bit unnecessary. This type of precision is usually reserved for things like applying a more precise force. Just set an explosion at the point where your projectile is when oncollisionenter is called and you've made everything much simpler.
on your instantiate you are calling a transform it should be a gameobject will this fix it I don't know but should help.
Answer by TonyLi · Jun 28, 2013 at 01:35 PM
Are both objects on layers that are configured for collision detection? (Check in Edit > Project Settings > Physics.)
http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
Do they both have colliders in addition to rigidbodies? The simpler the collider, the faster your game will run. Try adding a sphere collider to the asteroid and maybe a box collider to the ship.
Also, add a Debug.Log() line inside OnCollisionEnter() to make sure that the function is actually getting called.
Your answer
Follow this Question
Related Questions
SCRIPT NOT WORKING 2 Answers
ONGUI not working correctly, does code placement matter? 1 Answer
Whats wrong with this simple code? 2 Answers
Player Fast Travels 1 Answer