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 /
avatar image
0
Question by SyntaxTerror · Mar 08, 2014 at 03:33 PM · c#angleprojectileballbreakout

Setting the angle of a ball in a Breakout game

I'm having problems getting the ball to bounce off the paddle at the same speed, no matter the angle of deflection. It is my code, which I wrote quickly, but I'm not sure how to get it to work the way I want to.

Usually, whenever I'm writing a Breakout clone as a learning project, I set up a deflection table which allows me to tweak the angles and speed at will. However, on this occasion I've decided to try and get the ball to bounce at an angle determined by where it lands on the Paddle, and for it to calculate the angle on the fly rather than look for a pre-determined angle in the lookup table.

The simple code I have at the moment looks like this.

     void BallHitPaddle()
     {
         GameObject paddle = GameObject.Find("Player");
         float paddleX = paddle.transform.position.x;
         float ballX = gameObject.transform.position.x;
         float offsetX = ballX - paddleX;
 
         gameObject.rigidbody2D.velocity = new Vector2((offsetX * speed * 2), speed);
     }

Of course, this doesn't give the result I'm after. Although it deflects at nice angles, the acuter the angle, the faster the ball travels. I did find this: Angle and Velocity in a Projectile which looks like it could set me on the right path. Unfortunately I can't get it to work in C#. The compiler's throwing up all kinds of issues and I can't figure out a way around it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer Wiki

Answer by NoodleSpenny · Mar 09, 2014 at 03:38 PM

Your ball's velocity vector describes both its speed and its direction. The speed of the ball will be the magnitude of its velocity vector, i.e. sqrt(velocity.x^2 + velocity.y^2). You can see that the way you are setting the velocity now, the magnitude (speed) will be different depending on where the ball hits the paddle.

What you want to do is determine the direction of travel first as a unit vector (a unit vector has a magnitude of 1). Then you multiply that direction by your desired speed, and that is what you will set your ball's velocity to.

 float speed = 5.0f;

 void BallHitPaddle()
 {
     GameObject paddle = GameObject.Find("Player");
     Vector2 paddlePosition = paddle.transform.position;
     Vector2 ballPosition = gameObject.transform.position;
     
     // this is the vector 'pointing' from the paddle to the ball
     Vector2 delta = ballPosition - paddlePosition;

     // normalizing converts a vector into a unit vector
     // (i.e. a vector with the same direction, but a magnitude of 1)
     Vector2 direction = delta.normalized;

     // set the velocity to be the direction vector scaled to the desired speed
     rigidbody2D.velocity = direction * speed;    
 }
Comment
Add comment · Show 3 · 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 SyntaxTerror · Mar 09, 2014 at 06:46 PM 0
Share

Thanks a lot. That seems to work perfectly.

avatar image retr0rockit · Jul 03, 2018 at 01:43 AM 0
Share

Thanks for this info. I am new to Unity and game dev in general. Started out creating the simplest game I could think of, handball. I used a pong tutorial I found as a base project and built on that. I had things set up pretty good, but the ball movement was too predictable. I wrestled for a couple of days with how to get the ball direction to behave based on where it hit the paddle. Finally found this and it helped so much. Now I need to get my head around how it works so I will understand it.

avatar image Redroid · Nov 08, 2018 at 12:22 AM 0
Share

Wow this work like charm Thanks a lot sir.

avatar image
0

Answer by robertbu · Mar 09, 2014 at 03:23 PM

Try this

     gameObject.rigidbody2D.velocity = new Vector2((offsetX * 2), 1).normalized * speed;
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

23 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

Related Questions

How to get rotation for projectile to fire at cursor? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Shooting extra arrows at an angle from the first 1 Answer

Boomerang projectile doesn't return to player if the player has moved 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