- Home /
łatwy problem dla początkujących z kolizjami.
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
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 ShadyProductions · Jun 14, 2020 at 08:07 PM
make sure bullet has a collider of type trigger, and obstacle has collider. Add some logging to see if the trigger works:
private void OnTriggerEnter2D(Collider2D obj)
{
if (obj.gameObject.tag == "obstacle")
{
Debug.Log("We collided with: " + obj.gameObject.name);
Destroy(this.gameObject);
}
else
{
Debug.Log("We did not collide with obstacle but with: " + obj.gameObject.name);
}
}
Also it can depend on how this object moves, does it use Rigidbody velocity movement or vector translate movement. Physics does not register collisions if they come from Translate movement. They must come from rigidbody based movement.
$$anonymous$$y bullet move (translate) and my player move if input D new Vector 3 speed, player.velocity.y
$$anonymous$$ove your bullet also with velocity, it will fix your problem
thanks bro i tried this over this day and i forgot unity comunity and sorry my English i bad. Dzięki!
reply with console "We did not collide with obstacle but with: Player Clone becouse my player is in prefab folder and my script Instantiate it on Start and checkpoint
Your answer
Follow this Question
Related Questions
CompareTag being ignored? 1 Answer
how to teleport player to another gameobject using the players tag? 0 Answers
How can to convert it on c#? 1 Answer
Multiple Cars not working 1 Answer
RayCasts and BoxCollider2D not working? 2 Answers