2D C# Collecting Item For Acceleration Unity

I have an issue regarding acceleration for Unity in C#, I'm trying to have my player collect an item and gain speed from it. Now, I have this working, but I need it to be a gradual acceleration, I don't want my player to just instantly jump to the maximum speed like he is now.

Here is how far I've got, I'm not sure if I'm on the right track or not. Any help?

void OnTriggerEnter2D(Collider2D other) {

if (other.tag == "speedBoost") {

transform.position += Vector3.right * acceleration * Time.deltaTime;

if (acceleration < horizontalSpeed) {
acceleration += 1;
}
//transform.position += Vector3.right * horizontalSpeed * Time.deltaTime;
Destroy (other.gameObject);
}
}


• You will need to control it in an Update method not a detection method. You will use the detection method to determine when you should start speeding up. Jun 12, 2015 at 3:07
• Alright, how might one go about achieving that, specifically? Jun 12, 2015 at 3:11
• Rather than giving you the answer, I'll point you in the right direction. If I were to implement something like this, I would use docs.unity3d.com/ScriptReference/Mathf.Lerp.html You'll need to rearrange and redesign your implementation of this. I would suggest having a boolean value that is used to determine when to increase the acceleration. If you think about it, all you are doing is checking to see if something is true, and if it is then interpolate between your normal acceleration to your max acceleration over time. Understand? Jun 12, 2015 at 4:37
• Alright thanks, I've tried for a couple of hours to work that out but it's not coming together for me yet. If you could offer a few more clues that might help me. Jun 12, 2015 at 11:31

You can use Mathf.Lerp. Mathf.Lerp takes in 'From', 'To' and 'T' parameters. It interpolates your value From to To over T. The T parameter should be a value between 0 and 1. 0 is equal to your From value and 1 is equal to your To value.

For example:

From = 1

To = 2

Difference = To - From = 1

So if T were equal to 0.5, the returned value would be 1.5

I'm not entirely sure what your exact plans for this idea are or how it will tie into your game, but I have constructed a basic script for you to read over and understand what is happening.

using UnityEngine;
using System.Collections;

public class PowerUpTest : MonoBehaviour
{
public float minSpeed = 1.0f;   //Minimum powerup speed 'From'
public float maxSpeed = 10.0f;  //Maximum powerup speed 'To'
public float incrementRate = 0.1f;  //The rate you want the speed to change
public string powerUpName = "SpeedBoost";   //The tag of the powerup gameobject

private bool executePowerUp = false;  //Checks whether to execute the powerup
private float currentSpeed; //The current speed of your powerup boost value
private float lerpTime;  //The lerp time 'T'

void Start ()
{
currentSpeed = minSpeed; //Set current speed to equal the minimum speed
}

void Update ()
{
if (executePowerUp) //If the executePowerUp bool is true
{
if (lerpTime <= 1) //If the lerp time has not reached the maximum value ('To')
{
lerpTime += Time.deltaTime * incrementRate; //Increase the lerp time by deltaTime multiplied by the rate
currentSpeed = Mathf.Lerp (minSpeed, maxSpeed, lerpTime); //Set the current speed to the current lerped value
print (currentSpeed); //print the current speed for you to see the change in the value
}
else //If the lerp time has exceeded the 'To' value
{
executePowerUp = false; //Turn execute powerup to false
lerpTime = 0; //Reset the lerp time
}
}
}

void OnTriggerEnter2D (Collider2D other)
{
if (other.CompareTag (powerUpName)) //If the trigger object tag is equal to the powerup tag
{
executePowerUp = true; //Set execute power to true to initiate the lerp code
Destroy (other.gameObject); //Remove the collided object
}
}
}


transform.position += Vector3.right * acceleration * Time.deltaTime;

transform.position is in length units (it's value represents distances along the 3 world axes from the origin).
In this line of code you are adding a value with velocity units to a variable with length units. acceleration is length/time^2, Time. deltaTime is time, and Vector3.right is unitless (it's a direction). Multiplied together we get length/time which is velocity.
Perhaps the name of the acceleration variable is misleading and it's actually velocity. By any case, you have to make sure your units check out first. Then, to get the effect you want what you want to do is instead of adding values to the position of the object, add values to it's maximum speed (store this variable somewhere in a component).