Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 CryFan001 · Jan 04, 2019 at 02:59 AM · velocitybouncepongbouncingbounciness

Unity 2D - Why does velocity causes the ball to change direction everytime it hits something?

So, I'm following a youtube tutorial on how to make pong. I understand and learned alot of stuff but there's a point where he made the ball and its movement but he didn't explain it. He used

 void Start() 
 {
     rb.velocty = new Vector2(speed, speed);
 }

He also applied a rigidbody2d, a circle collider with a physic material in which the friction is set to 0 and bounciness to 1. This somehow made the ball bounce off an object.

Can anyone explained to me how it works?

Thanks alot!

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
1

Answer by Ady_M · Jan 05, 2019 at 10:33 PM

One of the purposes of a Rigidbody is to add realistic movement and automatic collision detection to your objects without having to do all the tedious work it requires to program it yourself.

 

Once you give an object a velocity (which is a combination of direction and speed), it will keep moving in that direction until something stops it. It could be drag, friction from moving along the ground/wall or an obstacle (another object that has a collider).

 

As you can tell, the velocity in the code you posted is only set once: in Start()

The fact that you've added a Rigidbody and set the object in motion using Rigidbody.velocity is enough to make the object move and bounce off of other colliders.

 

In case you're wondering why it says "speed" twice in the code you posted: If the speed is 10, for instance, then you've told it to move 10 units per second on the X-axis and 10 units per second on the Y-axis. Which means diagonally. I say "units" because you can call it whatever you want (inches, cm, meters, etc.).

 

The Physic Material was added to make sure that the object does not lose speed from touching the ground and bouncing off the wall/paddle. If you don't create a Physic Material and attach it to your object's collider then it will use the default Physic Material which doesn't bounce as much and it also absorbs a bit of speed with every collision.

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

101 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

Related Questions

Pong Mechanics 1 Answer

How to change bouncing direction 2 Answers

2D Polygon Collider Ball with bounce material not stay completely still 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How do I make a sphere bounce forward without adding a lateral force? 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