Hi,
Using an OnTriggerEnter to turn a variable true, I want to be able to alter a returned value, to make a speedboost. As it stands, the if statement doesn’t do anything, following a few other answers on here it looks like it should do.
I’m still fairly new with scripting, thanks in advance for any help.
function OnTriggerEnter(other:Collider)
{
if(other.gameObject.tag == "SpeedBoost")
{
speedboost = true;
Debug.Log("Hit");
}
}
function Convert_Miles_Per_Hour_To_Meters_Per_Second(value : float) : float
{
return value * 0.44704;
if(speedboost)
{
return value * 2;
}
}
First your “Convert_Miles_Per_Hour_To_Meters_Per_Second” function of course won’t work the way you have it since you return at the start of the function.
To get it working your would have to do something like this:
function Convert_Miles_Per_Hour_To_Meters_Per_Second(value : float) : float
{
if(speedboost)
{
value *= 2;
}
return value * 0.44704;
}
However:
Your function name is too long
Your function name is wrong!!! because it’s not obvious that it will add a speed boost based on some magic variable.
Debug.Log(
will cause a syntax error, so get rid of that to start with.
Does the object entering the trigger have both a rigidbody and the “SpeedBoost” tag?
Save some typing, and it’s clear - plus you can do things like taper the speedboost over time with a coroutine… Although to be fair ConvertMPH2MPS maybe needs a clearer name in that case, like ‘GetGameSpeed’