- Home /
plain problem with collider. begginer in unity
Hey! I made a 2D game based on shooting. The problem is that the bullets fly through everything! I used this void
void OnTriggerEnter2D(Collider2D obj) { if (obj.gameObject.tag == "obstacle") { Destroy(this.gameObject); } } But this not work D: information: Bullet is set on trigger, Obstacle is set on plain, box collider tag is correct, move in bullet and player is same
idk what i make wrong maybe i use wrong void or i wrong use this? Idk iam begginer in unity and c# if you wanna more information write thanks! Active and usefull friends @onlineight @ShadyProductions @xxnickiiixx @sarahnorthway
Answer by Artik2442 · Jun 15, 2020 at 07:29 AM
It is because of the speed of your bullet...
Your bullet go so fast that unity have some trouble to calculate the OnTrigger or OnCollision event!
The easiest way to fix that is to use rb2D.AddForce and not rb2D.velocity or transform.Translate(transform.right * speed).
With that, you have to find the perfect force speed so the trigger/collider can be detected by unity.
Hope it helps...
I forgot to tell that the rb2D.AddForce is for the movement of the bullet.
You can set the mesh collider of your plain to convex too!
Answer by TheNiluseqqq · Jun 15, 2020 at 12:26 PM
Releasing the bullet does nothing. Could you show me how to use rb2D.AddForce to make my object move? @Artik2442
When you are Instantiating the bullet, do the bullet has a Rigidbody2D Component?
Because your bullet will need one. Set the mass to a very low value, you can choose one you want (because your bullet will drop)
In your script, when you are instantiating:
GameObject bulletInstantiated = Instantiate(bullet, bulletPos, bulletRot); //Here you are instantiating the bullet
bulletInstantiated.GetComponent<Rigidbody2D>().AddForce(transform.right * ForceSpeed)//You are adding a force that will make the bullet movement
The ForceSpeed will be the value you will change to have the perfect speed of your bullet.
Are you on a 2D game? If yes, look if your plane Z position is equal to the Z position of your bullet.