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 /
  • Help Room /
avatar image
0
Question by Reverend-Speed · Nov 08, 2016 at 08:05 PM · vector3mathbouncelocalpong

Why does my Pong paddle code work?

This is unquestionably the dumbest question I've asked on this site. I was doing a Pong game a short while ago and spent time getting the ball to bounce away from the paddles in a direction related to where it touched the paddle.

And it seemed to work!

And I failed to comment my code.

And now I'm looking at the code and scratching my head as to how this gibberish does what it's supposed to do. Can anybody out there parse this for me?

X Axis is the length of the Pong court (the ball travels along this axis during a rally).

Z Axis is the width of the Pong court (the paddles travel this axis in order to return the ball).

The following code is on the ball:

 public void BounceBallRelativeToImpact(Vector3 paddlePos, float paddleWidth)
     {
         float a = transform.position.x - paddlePos.x; // Huh? Local *X* space?
         float x = a / paddleWidth; // HUH?? Why even, what does, guh?
 
         int zDir = 0;
         if (tf.position.z > paddlePos.z) // Straightforward, which direction should I bounce in.
         {
             zDir = 1;
         }
         else
         {
             zDir = -1;
         }
 
         direction = new Vector3(a, 0.0f, zDir).normalized; // ??? => PROFIT
     }

This is called via OnCollisionEnter from the paddle that touches the ball, passing in the paddle's position and the bounds.size.x from the paddle collider.

Sure enough, observation of the game in action suggests that this is doing what it's supposed to be doing. But:

Why are we getting the ball position in the paddle's z local space? Why are we dividing this by the paddle's WIDTH?!?! Is this to do with producing a magnitude that will be normalised later?

God, I'm cursing myself for not commenting this a while ago. It's fairly simple code, it seems to do the job, I don't understand it.

MORAL: Kids, comment your code.

Anybody out there feel like helping out a certified idiot? Thank you.

--Rev

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 Reverend-Speed · Nov 09, 2016 at 08:15 PM

Okay, so, insanity code aside, a nice Anonymous Benefactor (YOU KNOW WHO YOU ARE, ANON WINK) sorted out some reasonable, working paddle code, which I'll leave here for anybody looking for something similar.

 void OnCollisionEnter(Collision col)
     {
         direction = Vector3.Reflect(direction, col.contacts[0].normal); // Get a Vector3 which is a reflection of the ball's direction when it collided with an object...
 
         if (col.transform.CompareTag("Paddle")) // If we determine that the collided object is tagged 'Paddle', then we do a little more...
         {
             // (Ball.z - paddle.z) * the swerve coefficient
             float ballPaddleZDifference = transform.position.z - col.transform.position.z;  // Distance from the ball to the centre of the paddle.
             direction.z += ballPaddleZDifference * backspin;                                // Give the direction backspin dependant on where the ball hit the paddle.
             speed++;                                                                        // As the player has successfully returned the ball, increase ball speed!
         }
 
         aud.Play(); // Tell AudioSource to play 'blip'!
     }

You should be able to work out how to get things running from there. Again, thanks Anon!

--Rev

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

65 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

Related Questions

not the bounce iam looking for 2 Answers

Get a Vector3 to always have a radius of 1 from 0, 0, 0 2 Answers

GPD Pong - Trouble with Raycasting for Opponent's Look-Ahead AI 1 Answer

How to detect when an object is within an arc? 2 Answers

How do I get an object's velocity in one direction? 4 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