Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by Pulsar-Regions · Aug 13, 2013 at 03:41 PM · 2dcollisionprojectileballbounce

Bounce when hitting wall

Hey everyone I'm working on a simple 2d game, but I've run into a problem. Right now if you shoot a projectile and it hits the wall it will bounce perfectly. But if it hits another wall it screws up, which is perfectly makes sense. Why? Because that's how I coded it. Here is a example picture of the problem:

alt text

I need help to reformulate the code, and here it is:

 public class bullet : MonoBehaviour {
     
     public Vector3 velocity;
     public float speed;
     
 
     void Start () {
         velocity = transform.up;
         speed = 250;
         
         StartCoroutine (Wait (5));
     }
     
     void Update () {
         transform.position += velocity * Time.deltaTime * speed;
     }
     
     IEnumerator Wait(float seconds) {
         yield return new WaitForSeconds(seconds);    
         Destroy(transform.gameObject);
     }
     
     public void OnCollisionEnter(Collision owner) {
         if(owner.gameObject.tag == "wall") {
             
             foreach( ContactPoint contact in owner.contacts ) {
                 velocity = Quaternion.AngleAxis(180, contact.normal) * transform.up * -1;    
             }
         }
     }
 }
 
 


Comment
Add comment · Show 3
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 Mill0698 · Aug 13, 2013 at 03:46 PM 0
Share

Could your 2nd GameObject not have a trigger?

avatar image Pulsar-Regions · Aug 13, 2013 at 04:27 PM 0
Share

I'm not sure what you mean by "2nd gameobject"?

avatar image BVGuled · Jun 05, 2021 at 08:53 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);
         }
     }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IgorAherne · Aug 13, 2013 at 05:45 PM

using UnityEngine; using System.Collections;

public class move: MonoBehaviour {

 public Vector3 velocity;
 public float speed;
  bool flipped = false;
 
 void Start () {
    velocity = transform.up;
    speed = 3;
 
    StartCoroutine (Wait (5));
 }
 
 void Update () {
    transform.position += velocity * Time.deltaTime * speed;
     Debug.DrawRay(transform.position, velocity*10);
 }
 
 IEnumerator Wait(float seconds) {
    yield return new WaitForSeconds(seconds); 
    Destroy(transform.gameObject);
 }
 
 public void OnCollisionEnter(Collision owner) {
     Debug.Log ("Collision");
    if(owner.gameObject.tag == "wall") {
 
      foreach( ContactPoint contact in owner.contacts ) {
             Debug.Log ("going");
         if(!flipped){
       velocity = Quaternion.AngleAxis(180, contact.normal) * transform.up*-1;  
         flipped = true;
             }
         else{
          velocity = Quaternion.AngleAxis(0, contact.normal) * transform.up;
             flipped = false;
             }
      }
    }
 }

}

Comment
Add comment · Show 1 · 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 Pulsar-Regions · Aug 13, 2013 at 06:18 PM 0
Share

I made it kinda work by adding 'return' on both sides so it doesn't execute both. Anyways, sometimes it screws up the direction it's going, but it's atleast a step closer.

edit: added: rigidbody.freezeRotation = true; in start function and it's now working correctly thank you.

edit: well I noticed when it's colliding with a vertical wall and then a horizontal wall (and conversely) it does not bounce correctly. Anybody knows how to fix this?

avatar image
0

Answer by kirbygc00 · Jun 05, 2021 at 05:06 PM

https://docs.unity3d.com/ScriptReference/Vector3.Reflect.html Seems relevant

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

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Unity2D Collision is bugged 0 Answers

ball breaks through walls 1 Answer

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

Trajectory Projectile with Collision Detection 2 Answers

C# Projectile Ricochet After Collision2d 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