0
\$\begingroup\$

I am trying to shoot a bullet (rigidbody2d, boxcollider2d) to another enemy object (rigidbody2d, kinematic, circlecollider2d). I want the collision point between these two. When they collide, i want a collision particle effect and from the contact point, i want to create 5 rock objects all moving in different directions. How should i achieve this? Problem is that, the contact point i get seems incorrect. Collision particle effect is created when collision happens but the rock objects are created in some other point close to the contact point but not the contact point. Also how to i move the rock objects in different directions after instantiating them?

my code:

void OnCollisionEnter2D(Collision2D coll)
{
    if (coll.gameObject.tag == "Enemy")
    {
        ContactPoint2D contact = coll.contacts[0];

        GameObject rock1 =  Instantiate (rock, contact.point, transform.rotation) as GameObject;
        rock1.rigidbody2D.velocity = transform.up * 20;
        GameObject rock2 =  Instantiate (rock, newPos, transform.rotation) as GameObject;
        rock2.rigidbody2D.velocity = transform.right * 20;
        Destroy(coll.gameObject);
        Destroy (gameObject);
        Instantiate (explosion, transform.position, Quaternion.identity);           

    }
}
\$\endgroup\$
1
\$\begingroup\$
void OnCollisionEnter2D(Collision2D coll)
{
    if (coll.CompareTag("Enemy")) // more efficient
    {
        ContactPoint2D contact = coll.contacts[0];

        for(int i = 0; i < 5; ++i) // should probably be completely replaced with a particle system depending on your goals. 
        {
           GameObject rock =  Instantiate (rock, contact.point, transform.rotation) as GameObject;
           rock.rigidbody2D.velocity = Random.insideUnitCircle * 20;
        }

        Instantiate (explosion, transform.position, Quaternion.identity);

        Destroy(coll.gameObject);
        Destroy (gameObject);           
    }
}

If the contact point is incorrect then the issue is probably that your Enemy object is in a hierarchy and the contact point is in local space but your rocks are being created in world space. To fix this when you instantiate your rock you need to add the rock to the hierarchy or translate it so it matches the contact.point.

\$\endgroup\$
  • \$\begingroup\$ sorry i am a newbie, How can i know if any of my game object is in local space or world space? How do i translate? \$\endgroup\$ – Swati Rawat Mar 11 '15 at 12:22
  • \$\begingroup\$ In the unity editor in the hierarchy menu, are your enemy gameobjects a parent of another gameobject? If so the child(enemy) is inheriting the transform from its parent. But i am not sure if the contact point return from the Collision2D is in word space. You will need to test and see if this is the case. here is a good explanation of spaces stackoverflow.com/questions/19946652/… \$\endgroup\$ – Anthony Raimondo Mar 11 '15 at 12:37
  • \$\begingroup\$ To translate you can try, contact.point + coll.transform.position. when creating your rock. \$\endgroup\$ – Anthony Raimondo Mar 11 '15 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.