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.
EDIT:
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.