- Home /
How to stop a bullet (CircleCollider2D) from continuing to move after hitting a wall?
In my game canons fires different kinds of bullets with different velocities. With the fastest bullet I run in to problems (velocity = 40). When the bullet collides with something It does not get destroyed immediately (a delay for about 0.04s or something). Instead it bounces away and sometimes collides with other elements in the game (such as other walls, enemies etc). As you see in my code below I have tried some things that didnt fix the problem ( coll.gameObject.SetActive (false); and Destroy (coll.gameObject);). Can this problem be fixed or is this a "bug" in Unitys physics simulator?
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.tag == "bullet")
{
coll.gameObject.GetComponent<CircleCollider2D>().isTrigger = true;
coll.gameObject.GetComponent<CircleCollider2D>().enabled = false;
coll.gameObject.GetComponent<SpriteRenderer>().enabled = false;
coll.gameObject.GetComponent<ParticleSystem> ().Stop ();
coll.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2 (0,0);
coll.gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
// coll.gameObject.SetActive (false);
// Destroy (coll.gameObject);
}
}
Answer by melsy · Aug 13, 2017 at 06:12 PM
Just reread and saw you were changing the velocity. it might be because of the time steps of physics. it only runs at 50 frames a second so when it impacts it takes longer to work then Update() does
I would also try putt the collision stuff on the moving object instead of the stopped object
on the bullet
OnCollisionEnter2D(Collider coll) { if(coll.CompareTag("objectTag") { // set the componants on the local object GetComponent().Stuff; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612132632im_/https://answers.unity.com/themes/thub/images/avi.jpg)