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. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    I'd recommend 2 things to check, firs the Physics or 2dPhysics layers, make sure everything is testing collision properly.
    And second if you are instantiating a buttlet, check the start position of it, if you are using the player relative position and it starts from the middle of it, it might be starting inside a wall. Maybe move that starting point to an empty object with relative position of the bullet hole of the gun.
     
  3. Nathan_D

    Nathan_D

    Joined:
    Oct 10, 2013
    Posts:
    31
    Thanks for the reply! The thing is on other occasions I do want the bullet to collide with the wall like it is doing, but not when the wall is considered behind the player. I know it's all in 2D so 2D collisions will occur regardless of the depth I try to create, so that's why I'm trying to find a way to create that depth with the bullets as I've already done so visually.

    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. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    oh ok yeah i'd just temporarily disable collision for the start of the bullet if player is touching.the wall.