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;
}
}
I feel I should add, this is part of the car tutorial scripts from the unity store that I am working with.
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.
Uhm that's just a sample use case of your function... At some point you probably have a float value that represents miles per hour (in my sample SomeValue) and you want to convert the value to meters per second and additionally add a speed boost. The returned value has to be used somewhere (i just assigned it to a variable xxx).
Ah, in the tutorial I'm, using I think this is what your looking for. topSpeed = Convert_Miles_Per_Hour_To_Meters_Per_Second(topSpeed); This will bring the syntax error unknown identifier 'value'. I believe this is due to changing value to speed.
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?
My bad, didnt copy the full thing for the debug, there is no syntax errors in it. The debug works, it does collide. The issue was the speedboost doesn't work.
fixed it ;) btw that doesn't make sense either ;) The function should convert from MPH to m/s and additionally add a speed boost. i'm pretty sure the speed boost should be 2 times and not 4.473872... times ;)
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’
I feel I should add, this is part of the car tutorial scripts from the unity store that I am working with.
– Andyy_G