2D Collect Item C# Increase Acceleration

Hello all, I’m having an issue with collection a speed boosting item, I need to be able to collect a power up that increases my players speed a little more, but I don’t want it to be an instant jump like it is now, I need it to accelerate gradually.

Here is what I’ve tried so-far:

void OnTriggerEnter2D(Collider2D other) {

		if (other.tag == "SpeedBoost") {
			
			transform.position = new Vector2(Mathf.Lerp(minimum, maximum, Time.time));

			//transform.position += Vector3.right * horizontalSpeed * Time.deltaTime;
			Destroy (other.gameObject); 
		}
	}

Any ideas? All help is greatly appreciated!

Firstly Mathf.Lerp inside OnTriggerEnter2D() should actually be places inside the Update() method as OnTriggerEnter2D() will be executed for one frame only which is undesired for lerping. Also Lerp using Time.deltaTime instead of Time.time. Since as time passes after you started playing game the value of Time.time will be higher resulting in a jump inside of Lerp.

For position you should provide two values as you are using Vector2 where as currently you are using only one which is lerped.

And you can set the value of maximum inside OnTriggerEnter2D(), considering maximum is your maximum speed of player.

You are incorrectly using Mathf.Lerp. The Unity documentation on this is horrible. 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, 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
		}
	}
}

The code is fully commented. If you don’t understand something, post a comment.