I have read a lot of other topics both on here, on the Unity forums and from Google searches that answer thing that are similar but not quite the same as my question.

My question:

I have a Unity project with a sphere, the sphere starts at a certain initial speed and I want the sphere to accelerate uniformly over time to reach its maximum speed, regardless of bounces and interactions.

Obviously an interaction will cause the Velocity to change, but I don't want the Speed to change.

I started off looking for a speed LERP, but come up with the below code.

I have negated and turned off all drag and friction settings on the gameobject.

My current code is thus (C#):

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
    public float accelerationSpeed = 10f;
    public float maximumSpeed = 25f;
    public float initialSpeed = 10f;
    private Rigidbody rb;
    private bool ballInPlay = false;

    // Use this for initialization
    void Awake () {
        rb = gameObject.GetComponent<Rigidbody>();

    //...other functions that don't relate to this question... 

    void FixedUpdate ()
         * Gradually increase ball speed
        if (ballInPlay == true && rb.velocity.magnitude < maximumSpeed)
             rb.AddForce(rb.velocity.normalized * accelerationSpeed, ForceMode.Acceleration);


This code takes ~3-4 seconds to accelerate the ball up to the maximum speed, and it seems to adequately bounce the ball off rigidbodies, as I want.

However, I will in time be having more things interacting with the ball including explosions and other actual Force values (such as Wind), so taking this into account; is this code the best way of applying a constant acceleration to a game object, regardless of its other behaviours and influences?

While the above may work I'm also looking for best practise as far as possible (and memory/processor efficiency if that's applicable).

While obviously there are going to be unknowns (as it's excessive to show you my whole project, just for this qustion), some guidance on best methods of approach to solving this as well as improving efficiency would be appreciated.

Many Thanks.

To clarify my exact question:
Is the code above best practise when applying uniform acceleration to the gameobject while dealing with the influence of additional Rigidbody.AddForce functions on the gameobject.

  • \$\begingroup\$ StackExchange is usually better at finding a solution to a problem than at identifying "the best possible solution" - since, looking at a piece of code, how do we know if it's the best? Our vote system can evaluate pros & cons between multiple answers, but that still doesn't tell us whether any qualify as best practices. If your code works for you - great! Ship it! If you've encountered a problem with this code, describe the problem and we can try to solve it. \$\endgroup\$ – DMGregory Jan 2 '17 at 17:51
  • \$\begingroup\$ @DMGregory while I agree with you, I have found a lot of information on similar SE sites [stackoverflow] that people ask a question about a code problem, and replies tell us all how to use code with best practise. I am trying to reach that conclusion (best practise) of finding out the shape of a best practise approach before I run into a code issue. \$\endgroup\$ – Martin Jan 2 '17 at 19:05
  • \$\begingroup\$ @DMGregory Does my clarifier at the bottom of my question help clarify the question? \$\endgroup\$ – Martin Jan 2 '17 at 19:08
  • \$\begingroup\$ You'll usually find a well-defined "best practice" only for common features that many teams have implemented, sharing insights and refining approaches over time. For something like this, I don't think you're likely to find such an established standard. So, "if it works, use it" is a decent fallback. I can see minor improvements like clamping the acceleration so you never exceed maxSpeed, but that might not be significant for your use case — it's hard to know unless you can articulate something specific about this approach that you want to improve. \$\endgroup\$ – DMGregory Jan 2 '17 at 19:22
  • \$\begingroup\$ @DMGregory ok, thanks for your feedback. I'll try and relaunch this question (or if it's gone post anew) if I get into issues as I'm about to add explosions to my level. Cheers. also I was hoping there was a "Best practise" for acceleration (I figured accelerating was a common enough activity). \$\endgroup\$ – Martin Jan 2 '17 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.