Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by m_foxy · Jan 15, 2015 at 09:47 PM · 2dphysicsraycasthitbouncing

2D Bouncing formula doesn't work properly

I am new to unity, and i am trying to create a bouncing ball, so i've did many researches about bouncing realted physics and i found a formula :

Formula:

 -2*(V dot N)*N + V

Where V is the velocity vector and N is the normal of the surface on which the ball will bounce

Here is my script :

 using UnityEngine;
 
 using System.Collections;
 
 public class BallPhysics : MonoBehaviour {
 
 
 
     void Start () {
         rigidbody2D.velocity =new Vector2 (-1,-3);
 
     }
     
     // Update is called once per frame
     void Update () {
 
     }
     void OnTriggerEnter2D(Collider2D col) {
 
         if (col.gameObject.name == "Pong") {
             tBounce ();
                 }
         
 
     }
     
     void tBounce(){
         RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x,transform.position.y), new Vector2(-1f,-1f));
         Vector2 n = hit.normal;
         Vector2 v = rigidbody2D.velocity;
         Vector2 R = -2 * (Vector2.Dot (v, n)) * n + v;
         rigidbody2D.velocity = R;
     }
 }

I am giving the ball a velocity vector in the start function, i am using OnTriggerEnter2D for collision handling and raycast2D to get the normal of a surface.

The problem is that the script doesn't reflect the velocity vector called R, i think the probleme is in the normal vector.

For example let's say V is a Vector2(-1,-1) so basically R should be (-1,1), but it's not. R is (3,1) !

i've successfuly been able to make a ball bouncing on Horizontal/vertical surface by reversing the ball velocity but this won't work properly with arbitary angles,that's why i am using this formula. So what's the problem ?

Comment
Add comment · Show 2
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 moichezmoi · Jan 15, 2015 at 10:08 PM 0
Share

Hi, why is the direction of your raycast (-1, -1)? Shouldn't it be rigidbody2D.velocity?

avatar image m_foxy · Jan 15, 2015 at 10:15 PM 0
Share

Even if i use rigidbody2d.velocity the same problem occurs

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by skylem · Jan 15, 2015 at 10:40 PM

Hello, Quick question is there any particular reason as to why u wish to do this with code? You could set a Physics material property to your 2D Collider and the properties within this Physics Material Include Bounce and Friction.

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 m_foxy · Jan 15, 2015 at 10:43 PM 0
Share

The ball will bounce in the same place and i dont want it to be so realistic

avatar image
0

Answer by moichezmoi · Jan 15, 2015 at 11:05 PM

If you use the OnCollisionEnter2D method, you'll get a collision2D as parameter, that gives you more informations about the collision than the simple Collider2D you get right now with th OnTriggerEnter2D method.

You don't have to perform a raycast anymore :

 void OnCollisionEnter2D(Collision2D col) 
 {
     if (col.gameObject.name == "Pong") 
     {
         Vector2 n = col.contacts[0].normal;
         Vector2 v = col.relativeVelocity; 
         rigidbody2D.velocity = -2 * (Vector2.Dot (v, n)) * n + v;
     }
 }

I think the raycast was the problem : it interact with your ball's collider instead of your "Pong" object.

To use the OnCollisionEnter2D method, you have to untick "is Trigger" on your collider components.

Comment
Add comment · Show 5 · 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 m_foxy · Jan 15, 2015 at 11:10 PM 0
Share

I've already did this the problem with this is that the pong move when there is a collision with the ball,and the only way to dfx that is to tick isTrigger.

avatar image moichezmoi · Jan 15, 2015 at 11:13 PM 0
Share

oh, do you need to have a rigidbody on your Pong ?

avatar image m_foxy · Jan 15, 2015 at 11:16 PM 0
Share

I don't need it ,so without a rigidbody the pong won't move, right?

avatar image moichezmoi · Jan 15, 2015 at 11:16 PM 0
Share

no it won't

avatar image m_foxy · Jan 16, 2015 at 04:31 PM 0
Share

But there will be no collision at all

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2d physics problem 1 Answer

New Pointer Effector 2D Help 0 Answers

2D Ragdoll 1 Answer

Trying to get a ball to bounce realistically in 2D. 2 Answers

2D Collision not working! (child sprites colliders) 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