Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JayFitz91 · Apr 16, 2018 at 10:13 AM · collisionvelocitydirectionbounce

Moving a ball in the opposite direction of where it hits

Hi guys,

I am making a small arkanoid style game for practice. I currently have the ball bouncing around the screen with the following:

         if (viewPos.x < 0 || viewPos.x > Screen.width)
         {
             Vector2 vel = ballRB.velocity;
             vel.x = -vel.x;
             ballRB.velocity = vel;
         }
 
         if (viewPos.y < 0 || viewPos.y > Screen.height)
         {
             Vector2 vel = ballRB.velocity;
             vel.y = -vel.y;
             ballRB.velocity = vel;
         }

I am now at the part where I need to bounce the ball off the bricks. I determine what side of the brick was hit (currently using the bottom of the brick) and want to bounce the ball in the opposite direction.

If I'm not mistaken, should this code not be the same as the y position above? As i just want to bounce it back in the opposite direction, which is already what happens when the ball hits the top of the screen, instead, when I run the code below and the ball hits the underside of a brick, the ball just stops dead in it's tracks

 private void OnColliderEnter2D(Collider2D col)
 {
     if(col.gameObject.CompareTag("Brick"))
     {
         //bottom of the brick
         if (transform.position.y <= col.transform.position.y - (col.bounds.size.y / 2))
         {
             Vector2 vel = ballRB.velocity;
             vel.y = -vel.y;
             ballRB.velocity = vel;
 
             Destroy(col.gameObject);
         }
     }
 }

I know I could bounce the ball using a physics material but I would like to try do it without one.

Thanks in advance

Comment
Add comment
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 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JayFitz91 · Apr 16, 2018 at 01:32 PM

Just in case anyone was wondering, I managed to solve this by using the relative velocity of the colliding object, which bounces it back in the opposite direction the ball was going.

 ballRB.velocity = new Vector2(-col.relativeVelocity.x, col.relativeVelocity.y);
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

124 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

Related Questions

move 2D object in opposite direction after collision 1 Answer

How to make my moving object bounce 2 Answers

Unity 2D - How do I add the velocity and direction of an object to another object? 2 Answers

Unrealistic bounce 2 Answers

Avoid "bouncing" when colliding 2 Answers


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