Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bullet collides with wall behind player

Discussion in 'Scripting' started by Nathan_D, Jul 10, 2018.

  1. Nathan_D

    Nathan_D

    Joined:
    Oct 10, 2013
    Posts:
    31
    Hi everyone,

    I'm messing around with a 2D/2.5D isometric shooter like Nuclear Throne for practice and my player is having problems shooting while "hugging" a wall. My bullets collide with walls and are destroyed, but when my player shoots downward or to the side while he's touching the wall, the bullet collider touches the wall collider and acts like a collision and destroys itself.

    How do I address this? Would the wall collisions have to read velocity of the bullet? Layering doesn't work because I still want the bullet to collide with other walls, so I'm not sure how to code restrictions on the wall and bullet collision.

    Thanks!
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    First of all give the forum some time ;) you double posted. Could you show some code as this is scripting support and explain what is where happening?
     
  3. Nathan_D

    Nathan_D

    Joined:
    Oct 10, 2013
    Posts:
    31
    SO sorry for double posting, I thought my computer hung so I just pressed it twice without refreshing :S

    Here's my projectile collision code for hitting when it collides with a wall. This works as intended, but when my player is hugging a wall and shoots downwards, the bullet collides with the wall that's supposed to be behind him, and destroys the bullet.

    Code (CSharp):
    1.     public void OnCollisionEnter2D(Collision2D other) //Added 2D to calcualtion
    2.     {
    3.  
    4.         if (other.collider.tag == "Wall")
    5.         {
    6.             if(!collided)
    7.             {
    8.                 //print("FLAT WALL");
    9.                 collided = true;
    10.                 WallClass wallCall = other.collider.GetComponent<WallClass>();
    11.  
    12.                 ContactPoint2D collisionPoint = other.contacts[0];
    13.                 //sprint(other.contacts[0].otherCollider.name);
    14.                 HitFX(collisionPoint.point, wallCall);
    15.                 if (wallCall.WallFacingID == 1)
    16.                 {
    17.                     ChooseRandomBulletHole(collisionPoint.point, wallCall);
    18.                 }
    19.  
    20.                 ProjectileDestroyAction();
    21.  
    22.             }
    23.         }
    24. }

    If you can see Nuclear Throne @0:33, the player hugs the wall and shoots but the bullet does not collide with the wall even though in my game it does. Here is a visual example of my game situation:



    So its reading the collision, which it is supposed to, but the bullet logically should never collide with a wall behind it when it is shot at this angle. I still want the bullet to collide if the player were to be shooting upward at this wall. Do I have to check the velocity of the bullet to determine what is allowed to be collided with the wall? Thanks in advance.
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Shouldn't a tech company like UT be able to create a click guard so it's not possible? :)