Speed Player up on PickUp

Evening all,

I’m putting together an autoscrolling platformer, well trying to anyway… I have a pick up that slows the player down and I’m currently trying to add a pick up that counteracts the effects. I thought the best way to do this (with my limited coding knowledge), would be to use the same script over and reverse the content, or try to. However when the player runs into the speed up trigger it slows him down further. Here’s my (working) slow down script -

public float SlowDownPercentage = 0.5f;
	public bool hasSlowedPlayer = false;
	
	
	void OnCollisionEnter(Collision collision)
	{
		if(collision.collider.tag == "Player"  !hasSlowedPlayer)
		{
			collision.collider.GetComponent<PlayerMove>().speed *= SlowDownPercentage;
			hasSlowedPlayer = true;

And here’s what I’ve tested to reverse the effects -

public float SpeedUpRate = 0.5f;
	public bool hasSpedUp = false;
	
	
	void OnCollisionEnter(Collision collision)
	{
		if(collision.collider.tag == "Player"  !hasSpedUp)
		{
			collision.collider.GetComponent<PlayerMove>().speed *= SpeedUpRate;
			hasSpedUp = true;

I have also tried this with ‘is trigger’ enabled -

	public float SpeedUp = 0.5f;
	public bool hasSpedPlayer = false;
	
	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Player"  !hasSpedPlayer)
		{
			other.GetComponent<PlayerMove>().speed *= SpeedUp;
			hasSpedPlayer = true;

I’m pretty new to coding and the big scary world of Unity, so I’m trying to keep it simple as possible… Something I can understand really. It’s worth noting, both scripts I have tried work to slow the player down more, instead of speeding up. And ‘Has Sped Player’ in inspector changes to true once player has passed through. However with OnTriggerEnter script the trigger does not disappear as it should. Any suggestions how I can speed my player back up? Thanks in advance :wink:

well if you want to speed up the person back to its original speed, you should always have a base speed available
eg:

public float baseSpeed = 5.0f;

then when you hit a speed up variable
you have two options
option one:

if(other.getComponent<PlayerMove>().speed < other.getComponent<PlayerMove>().baseSpeed)
{
     other.getComponent<PlayerMove>().speed = baseSpeed;
}
else
{
     other.getComponent<PlayerMove>().speed /= 0.5f;
}

notice my “/=” basic math, if you multiply by a number less than 1 you divide by its inverse (eg if you multiply by .5 you divide by 2 and if you divide by .5 you multiply by 2).

or option 2

other.getComponent<PlayerMove>().speed /=0.5f;