Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bouncing Bullets Not Working

Discussion in '2D' started by reallybasicgames, Mar 26, 2021.

  1. reallybasicgames

    reallybasicgames

    Joined:
    Jun 26, 2020
    Posts:
    3
    Hello!

    I am trying to make bullets ricochet off of metal objects, but not off of anything else. Right now it collides with everything fine, but after hours of looking at tutorials, this feature still will not work. The bullets simply travel through anything that is metal.

    A few things to mention: the bullets are using the trigger collider, because if they didn't they interact with the rigidbody on the tank. If you can think of a way for the bullets to NOT interact with the tank, but still use the 'OnCollisionEnter' method, I am open to this as a solution as well. Second, the environment the bullet is interacting with is in the collisionMask var on line 17, so that is not the issue. Third, when debugging, the only thing that is printed is "Hit metal" from line 15.

    Here is my code:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         try
    4.         {
    5.             if (collision.gameObject.GetComponent<Actor>() != cameFrom)
    6.             {
    7.                 collision.gameObject.GetComponent<DamageHandler>().TakeDamage(damage);
    8.                 if (!collision.gameObject.GetComponent<DamageHandler>().IsMetal())
    9.                 {
    10.                     Destroy();
    11.                 }
    12.                 else
    13.                 {
    14.  
    15.                     Debug.Log("Hit metal");
    16.                     Ray ray = new Ray(transform.position, transform.forward);
    17.                     if(Physics.Raycast(ray, out RaycastHit hit, Time.deltaTime * bulletSpeed +0.1f, collisionMask))
    18.                     {
    19.                         Debug.Log("Before Reflection: " + transform.eulerAngles);
    20.                         Vector2 reflectDir = Vector2.Reflect(ray.direction, hit.normal);
    21.                         float rot = 270 - Mathf.Atan2(reflectDir.y, reflectDir.x) * Mathf.Rad2Deg;
    22.                         transform.eulerAngles = new Vector3(0, 0, rot);
    23.                         Debug.Log("After Reflection: " + transform.eulerAngles);
    24.                     }
    25.                 }
    26.  
    27.             }
    28.         }
    29.        
    30.     }
    Thanks for your help!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    This is 3D physics not 2D physics. It won't detect ANY 2D colliders, only 3D ones.

    You should use Physics2D. Please note that there's a Physics forum for this stuff.
     
    reallybasicgames likes this.
  3. reallybasicgames

    reallybasicgames

    Joined:
    Jun 26, 2020
    Posts:
    3
    Ok, thank you! I am new to the unity forums and still trying to find my way around. I will look into physics2D and post in that forum if I still need help.
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    No problem, just making you aware of that forum as it can be very useful.

    Good luck.