last day (15 days later) » 

6:03 PM
0
A: Bullet flies inside wall, OnCollisionEnter2D problem

Ignacio AlorreYou should add as well OnTriggerStay2D then, since the bullet is already inside the wall collider, no collision between colliders will be detected. void OnTriggerStay2D(Collider2D col) { if (col.gameObject.tag == "Wall") { Destroy(gameObject); } } Another option could be to...

 
Now it just flies thru all walls
 
@ЯрославСмирнов I edited, try adding as well this other trigger
 
Nah, it's don't work, OnTriggerStay2D doesn't glow blue like OnTriggerEnter2D or OnCollisionEnter2D
I don't use 'Is trigger' in collider model by the way
 
@ЯрославСмирнов try my last edit
 
3rd doesn't work
1st: Script error: OnTriggerStay2D This message parameter has to be of type: Collider2D
 
6:03 PM
@ЯрославСмирнов About the 1st error you say. Is it possible you just replaced the name of the function from OnCollisionEnter2D with OnTriggerStay2D but you kept the Collision2D instead Collider2D as parameter type?
@ЯрославСмирнов Besides, have you consider to make the is trigger collider active in the wall, so when the bullet collider is detected, it destroy the bullet GameObject?
 
Yes I changed. It's worse. Also my tank can go thru the wall xd. And bullet destroys not instantly
 
@ЯрославСмирнов write how you instantiate the bullet in your question
 
@Ignacio did it but I think it's not very useful
 
Hi
So what I was thinking was that you may be instantiating the bullet a little bit further than your tank
a few units or so
so what you could try is to generate a 2d Raycast from the tank to the position where you were planing to instantiate the bullet
 
Yes, its because it can collide with tank when you move up
 
6:06 PM
and in case that raycast collides with the wall, directly dont instantiate the bullet
so instead of destroying the bullet, you just dont instantiate it if it is going to appear inside the wall
that should even make the code more efficient, since you skip an instance and destruction of the bullet
let me check how to write that, since I never tried myself
 
I need to shot because the same problem with tank. I spawn bullet inside the enemy tank and then tank should get damage
 
but you can detect if it is the wall
ok I will rewrite all my answer with a possible approach
write there the code you are using to instantiate the bullet
 
Instantiate(bullet1, firepoint1.position, firepoint1.rotation);
 
ok
you instantiate the bullet from a script in the tank?
 
6:24 PM
ok Try that thing, and let me know if it gives you troubles
do you know how to set a game Object to a particular layer?
 
I don't know
Physics2D project settings?
 
So you need to add the object into a layer first
then you assign the wall to that layer in the editor
you can also follow this short tuorial
 
good answer but I neet to spawn bullet inside tank
and then it should take damage, bullet destroys
 
inside enemy tanks?
ok as I imagineç
there are enemy tanks, player tank and wall
so you add the wall in the wall layer
but not the enemy tank
so it is possible to instantiate the the bullet inside enemy tank and destroy them, it will just avoid instantiate bullet inside wall
 
6:39 PM
yes, let me know how it goes, we can keep here until we get it haha
 

last day (15 days later) »