- Home /
Projectile Collision help
Hey, I am playing around with a projectile here and this function wont work (the particle wont instantiate). Thank you in advance:
//Variables (START)_________
//The particles that will Instantiate on hit
var mudParticle : GameObject;
//Variables (END)___________
function OnCollisionEnter (hit : Collision)
{
if(hit.transform.tag == "floorMud")
{
Instantiate(mudParticle, transform.position, transform.rotation);
transform.renderer.enabled = false;
gameObject.GetComponent(SphereCollider).enabled = false;
}
}
Answer by Supershandy · May 30, 2013 at 09:19 AM
If it's a case of the bullet only being detected when it is travelling slow, but you want your bullet to move fast, you may want to do a raycast from the bullet with a distance of one. I had a similar problem on my first ever demo game and my bullets just passed through objects.
A sample code would be
var bullet : Transform; var speed : float = 50.0//Or whatever you want it to be
Function Update ()
{ var hit = RaycastHit;
transform.Translate (0, 0, speed * Time.deltatime);
if (Physics.Raycast (transform.position, transform.forward, hit, 1) { if (hit.transform.tag == "Something")//replace with tag of object you want to destroy { Destroy (gameObject); } } }
This will mean that when the Raycast hits something with the tag of your object, it will react as soon as it hits and more accurately then the bog-standard colliders that Unity provides.
Okay thanks! You were right, it only works when the bullet is travelling slowly. Because this is my first ever game, ill try both RayCast and colliders, so that I learn how both work. and decide on which I like best. This looks promising for getting collider systems to work!
Answer by ricardo_arango · May 25, 2013 at 11:57 AM
Try debugging you code with MonoDevelop. You can add a breakpoint on line with the if (...) statement.
You can then check if the tag of the hit object is "floorMud" and verify that "mudParticle" isn't a null reference.
http://docs.unity3d.com/Documentation/Manual/HOWTO-MonoDevelop.html
Okay, I did some debugging and a hit was not recognized/the function was not executed. I know that its a while since I last commented and I'm sorry, but it would be REALLY appreciated if you could help me with this...Thanks!
To get the collision events the script needs to be attached to the object that is colliding. You also need to make sure the collision layers are correct : http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
So with the object that is colliding, meaning the Bullet being fired I presume? Checked on the Layers and the collision $$anonymous$$atrix and it should be working...
Either one of them will receive the collision event if the script is attached to them.
Hmmm...so clearly it SHOULD work but it doesn't. I doubt that this is a bug in Unity im guessing people must have used this type of system before. Guessing its similar to this: http://answers.unity3d.com/questions/48114/smoke-particle-on-point-of-collision.html
Your answer
Follow this Question
Related Questions
Snap to Surface 1 Answer
How can I check if an instantiated object collides with another instantiated object? 1 Answer
How to read particle information from a OnParticleCollision call ? 0 Answers
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer
Making the collider change after being instantiate 2 Answers