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;

}

}
``````

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.
}

}
``````

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)

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;