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 Elayekay · Dec 31, 2014 at 07:19 PM · ballpongaccelerate

How to accelerate the ball in a pong clone

Hi all. I'm trying to accelerate the ball in a pong clone but I can't figure out how. I have no idea what I'm doing :(

 public class Ball : MonoBehaviour {
 
     public float initialVelocity = 2.0f;
     public float maxVelocity = 100.0f;
 
     public float accelerationRate = 2.0f;
 
     void Start () 
     {
                 rigidbody2D.velocity = (Vector2.one.normalized * initialVelocity);
         }
     void FixedUpdate()
     {
         while (rigidbody2D.velocity < maxVelocity) {??
 
                 }
         initialVelocity = rigidbody2D.velocity * accelerationRate;
 
     }
 
 }
Comment
Add comment · Show 3
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 Graham-Dunnett ♦♦ · Dec 31, 2014 at 07:19 PM 0
Share

C l e a r l y.

avatar image Qasem2014 · Dec 31, 2014 at 07:34 PM 0
Share

pong ? you mean on a hit ? is your game 2d ? do you know whats a bouncy physic ?

avatar image Elayekay · Dec 31, 2014 at 10:17 PM 0
Share

I just want the ball to accelerate slowly over time to make it more difficult for players as time goes on.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tetheta · Jan 01, 2015 at 01:44 AM

Okay, a few things to think about here.

First off, the Start function is called once, when the Game Object is first created. At that point, you're setting the velocity to whatever initialVelocity is multiplied by your normalized vector. This means the ball will start out at a velocity of 2.

Now, FixedUpdate is called a set number of times per second, but your code in it is a bit odd. First off, you have a while loop. A while loop will keep doing whatever is in it until the condition of the loop returns true. In this case it would repeat what's in the loop until rigidbody2d.velocity is >= maxVelocity -- probably what you want.

Secondly, you don't actually have anything in the while loop. The braces (one of which you have commented) enclose whatever you want in that loop. Those lines of code there serve no purpose whatsoever.

Third, while you're setting initialVelocity to something new, you never assign your rigidbody2D.velocity to initialVelocity, so nothing happens.

To fix this:

First off, fixedUpdate is already called a bunch of times a second, so there is no reason to use a while loop in it. An if statement would work fine.

Secondly, you want to set rigidBody2D.velocity, not initialVelocity.

Third, you probably want to add 2, not multiply by 2 (otherwise your acceleration will be insanely fast).


So I was in a rush and didn't test the code, turns out I'm a dummy and was assuming 1-D motion for some odd reason. In order to get 2-D motion working, you need to multiply the rigidbody2D.velocity.normalized by the desired speed. An example of how this would be done is below (and more explanation in the comments). I actually tested this code and it seemed to work well in a basic new unity scene.

     using UnityEngine;
     using System.Collections;
     
     public class Ball : MonoBehaviour
     {
         //I'm calling these speed, since technically velocity is a vector, speed is a scalar, and the difference is important here
         public float initialSpeed = 0.2f;
         private float curSpeed;
         public float maxSpeed = 10f;
     
         public float accelerationRate = .1f;
     
         void Start()
         {
             curSpeed = initialSpeed; //Update curVelocity
             rigidbody2D.velocity = (Vector2.one.normalized * curSpeed); //Set our starting velocity
             //Vector2.one.normalized should start it going up and to the right at a 45 degree angle.
         }
         void FixedUpdate()
         {
             if (curSpeed < maxSpeed)
             {
                 curSpeed += accelerationRate;
             }
             rigidbody2D.velocity = rigidbody2D.velocity.normalized * curSpeed; //Takes the velocity direction and multiplies it by our 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 Elayekay · Jan 01, 2015 at 05:47 AM 0
Share

Thanks very much for your patient and detailed response Tetheta. I feel like I understand what's going on a bit more now. However, the game now won't start and I'm getting two errors in the console: 'Assets/Ball.cs(40,33): error CS0019: Operator <' cannot be applied to operands of type UnityEngine.Vector2' and `float'

and then another error message that is the same except for the '+=' operator. I edited my code with your corrections, then to make sure I didn't type anything wrong I commented it out and copy and pasted yours in and it still didn't work. Any further help would be appreciated!

avatar image EvilTak · Jan 01, 2015 at 02:41 PM 0
Share

@Tetheta I think you should at least try your code out before posting your answer. That way you wouldn't cause extra problems to the OP like @Elayekay said in his comments.

avatar image Tetheta · Jan 01, 2015 at 09:27 PM 0
Share

@Evil Tak @Elayekay $$anonymous$$y apologies, I ended up having to run as I was writing this so I posted it quickly without checking.

I was thinking for some reason that you were only accelerating in one direction, since this obviously isn't the case things are a bit more complex. A similar issue was talked about here for limiting the speed: http://answers.unity3d.com/questions/749319/question-regarding-ridigbody2dvelocitymagnitude.html

The issue comes in when you want to accelerate in (I'm assu$$anonymous$$g) the direction the ball is already going. To do this, you need to get the normalized velocity (which basically returns how much in each direction the ball is going, with a total velocity of one) and then multiply that by your desired speed.

If you want to be fast (which you usually do), calling sqr$$anonymous$$agnitude and comparing that to the square of your maximum is actually best practice, but you can use the direct .magnitude call if that makes more sense to you. However I didn't end up actually using that in my code.

In order to do all this in what you have, you should create a new value to store the current speed, and multiply by that in FixedUpdate ins$$anonymous$$d of directly assigning to your velocity. You'll also increase (+= should work with this one) that value every time until the maximum. The code would look like the following:

     using UnityEngine;
     using System.Collections;
     
     public class Ball : $$anonymous$$onoBehaviour
     {
         //I'm calling these speed, since technically velocity is a vector, speed is a scalar, and the difference is important here
         public float initialSpeed = 0.2f;
         private float curSpeed;
         public float maxSpeed = 10f;
     
         public float accelerationRate = .1f;
     
         void Start()
         {
             curSpeed = initialSpeed; //Update curVelocity
             rigidbody2D.velocity = (Vector2.one.normalized * curSpeed); //Set our starting velocity
             //Vector2.one.normalized should start it going up and to the right at a 45 degree angle.
         }
         void FixedUpdate()
         {
             if (curSpeed < maxSpeed)
             {
                 curSpeed += accelerationRate;
             }
             rigidbody2D.velocity = rigidbody2D.velocity.normalized * curSpeed; //Takes the velocity direction and multiplies it by our speed.
         }
     
     }

I tested that code on a simple sprite with a rigidbody2D and it worked for me (hence the adjusted initial/etc values, change those back to whatever you want). I'll update my answer to reflect these changes.

Sorry about the earlier failure, I'm apparently pretty incapable without an editor yelling at me about basic things haha.

avatar image
0

Answer by tanz94 · Jan 01, 2015 at 07:50 AM

In the above code rigidbody2D.velocity cannot be compared to maxvelocity since maxvelocity is scalar and the other is vector quantity.`Use this

if (rigidbody2D.velocity.magnitude < maxVelocity)

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 Elayekay · Jan 01, 2015 at 01:01 PM 0
Share

Thanks Tanz. That seemed to deal with the '<' error message, but not the '+=' one. I tried adding magnitude inside the if statement : rigidbody2D.velocity.magnitude += accelerationRate; but then I got this error message: Assets/Ball.cs(18,46): error CS0200: Property or indexer `UnityEngine.Vector2.magnitude' cannot be assigned to (it is read only)

avatar image
0

Answer by EvilTak · Jan 01, 2015 at 02:38 PM

To accelerate the ball, use rigidbody.AddForce. Look at the following implementation of your script:

 public class Ball : MonoBehaviour {
     public float initialVelocity = 2.0f;
     public float maxVelocity = 100.0f;
     public float accelerationRate = 2.0f;
 
     void Start ()
     {
         rigidbody2D.velocity = (Vector2.one.normalized * initialVelocity);
     }
 
     void FixedUpdate()
     {
         Vector2 normalizedVel = rigidbody2D.velocity.normalized;
         rigidbody2D.AddForce(normalizedVel * accelerationRate);
         
         if(rigidbody2D.velocity.sqrMagnitude > maxVelocity * maxVelocity)
             rigidbody2D.velocity = normalizedVel * maxVelocity;  //This limits the velocity once it goes over maxVelocity
     }

AddForce is much better than modifying the velocity directly.

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 Elayekay · Jan 01, 2015 at 06:07 PM 0
Share

Thanks Tak. I tried that and the ball went crazy fast. There appeared to be three of them! Tried changing the speed values but it didn't help. I think it might not be a good idea to up the speed every frame (I assume this is happening when you try to accelerate the ball in FixedUpdate). I've been trying out Time.deltaTime like so: public class Ball : $$anonymous$$onoBehaviour {

     public float initialVelocity = 2.0f;
     public float maxVelocity = 100.0f;
     
 
     void Start () 
     {
                 rigidbody2D.velocity = (Vector2.one.normalized * initialVelocity);
 
         }
     void FixedUpdate()
     {
         if (rigidbody2D.velocity.magnitude < maxVelocity) 
         {
                     initialVelocity += 2.0f * Time.deltaTime;
 
                 }
 
 
 
     }

but it's not working. When I start the game the ball stays at the same speed, even though the value of the Initial Velocity goes up in the inspector!

avatar image EvilTak · Jan 01, 2015 at 06:30 PM 0
Share

Ins$$anonymous$$d, try using this in my code: rigidbody2D.AddForce(normalizedVel * accelerationRate * Time.fixedDeltaTime); and decrease the value of accelerationRate in the inspector. That should work fine. In your other code, there isn't any way you can set the value of initialVelocity equal to the rigidbody's velocity. Your code is completely wrong.

avatar image Elayekay · Jan 01, 2015 at 06:54 PM 0
Share

Thanks. No luck again. The ball doesn't speed up over time whatever values I enter, and even when acceleration is at 1 it's still much faster than the initial velocity '2'

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Curve a Ball in 3d space 4 Answers

Ball stuck horizontally between rackets in pong 0 Answers

Prevent ball from going straight on x and y axis 2 Answers

What is the best way to move a paddle accurately in a circle? 2 Answers

Ball Dropping, once hit make it go diagonal 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