• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by unity_4ErgdcF4dqxeIA · Oct 15, 2019 at 05:26 PM · 2dprojectilereflectionbounce

Unity 2D-¿How do I make a bullet bounce off the wall?

Hi, this problem has been anoying me for weeks, since i just found solutions for this but with 3D, or using raycast, and since i want the bullet to bounce of the wall and make the player able to react i was a little loss(as far as i know raycast doesnt allow this, but im a nooby in programming and unity so i could be wrong). Ideal Bounce: alt text

Here is my Script for the bullet:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     public GameObject hitEffect1;
     public GameObject hitEffect2;
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         GameObject effect = Instantiate(hitEffect1, transform.position, Quaternion.identity);
         Destroy(effect, 0.3f);
         Destroy(gameObject);
         //Accediendo a la variable del Script "Weapon", para que se añada una municion al contador cada vez que explota una bala.
         GameObject TanqueVerde = GameObject.Find("TanqueVerde");
         Weapon BulletScript = TanqueVerde.GetComponent<Weapon>();
         BulletScript.Munición += 1;
 
         if (collision.gameObject.name == "Enemigo01")
         {
             Destroy(gameObject);
             //Eliminando el efecto de impacto 1 que solo debe funcionar con las paredes, asi solo se ve el efecto de destrucción del enemigo"
             Destroy(effect, 0.0f);
 
         }
        if (collision.gameObject.name == "Pared")
         {
            
 
         }
         
 
 
     }
 
    
 }

And just in case here is my script that is attached to the player:

 using UnityEngine;
 public class Weapon : MonoBehaviour
 {
     public Transform FirePoint;
     public GameObject BalaPrefab;
     public float bulletSpeed = 20f;
     //Haciendo una condicion para poder disparar
     public bool canShoot = true;
     public int Munición = 3;
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             if (canShoot == true)
             {
                 Shoot();
                 Munición -= 1;
             }
                 
         }
         
         if (Munición >=1)
         {
             canShoot = true;
         }
 
         if (Munición <= 0)
         {
            canShoot = false;
         }
     }
     void Shoot()
     {
        GameObject bullet = Instantiate(BalaPrefab, FirePoint.position, FirePoint.rotation);
         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
         rb.AddForce(FirePoint.up * bulletSpeed, ForceMode2D.Impulse);
     }

Any help would be apreciated, thanks.

bullet.png (167.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BVGuled · Jun 05, 2021 at 08:51 AM 0
Share

please check the below code, apply it to bullet which should have rigidbody with gravity 0 and a circle collider 2d

  public float speed;
 
     private void Update()
     {
         transform.Translate(Vector2.up * Time.deltaTime * speed);
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.gameObject.tag == "wall")
         {
             ContactPoint2D point = collision.contacts[0];
             Vector2 newDir = Vector2.zero;
             Vector2 curDire = this.transform.TransformDirection(Vector2.up);
 
             newDir = Vector2.Reflect(curDire, point.normal);
             transform.rotation = Quaternion.FromToRotation(Vector2.up, newDir);
         }
     }

7 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zinios · Jun 02, 2021 at 05:10 PM

this is for 2d game - attach this script to bullet which have collider2d and rigidbody

     private void Update()
     {
         transform.Translate(Vector2.up * Time.deltaTime * 1);
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.gameObject.tag == "wall")
         {
             ContactPoint2D point = collision.contacts[0];
             Vector2 newDir = Vector2.zero;
             Vector2 curDire = this.transform.TransformDirection(Vector2.up);
 
             newDir = Vector2.Reflect(curDire, point.normal);
             transform.rotation = Quaternion.FromToRotation(Vector2.up, newDir);
         }
     }




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by BVGuled · Jun 02, 2021 at 05:32 PM

alt texti have created simple scene with 4 wall and a bouncing bullet with rigidbody and cicle collider. i have used translate function to move to bullet not the velocity.

 public float speed;
     private void Update()
     {
         transform.Translate(Vector2.up * Time.deltaTime * speed);
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.gameObject.tag == "wall")
         {
             ContactPoint2D point = collision.contacts[0];
             Vector2 newDir = Vector2.zero;
             Vector2 curDire = this.transform.TransformDirection(Vector2.up);
 
             newDir = Vector2.Reflect(curDire, point.normal);
             transform.rotation = Quaternion.FromToRotation(Vector2.up, newDir);
         }
     }



screenshot-148.png (192.4 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bounce when hitting wall 2 Answers

How do i make something reflect in 2D? 1 Answer

ball breaks through walls 1 Answer

(2D) Face Object in Direction of Travel 1 Answer

How to make ball bounce off screen boundaries? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges