- Home /
Help returning a true varaible
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.
Answer by Bunny83 · Nov 19, 2013 at 12:46 PM
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.
You should do something like:
function ConvertMPH2MPS(speed : float) : float
{
return speed * 0.44704;
}
function AddSpeedBoost(speed : float) : float
{
if(speedboost)
return speed * 2;
return speed;
}
And use something like:
xxx = ConvertMPH2MPS(AddSpeedBoost(someValue));
or
xxx = AddSpeedBoost(ConvertMPH2MPS(someValue));
which would be the same since the speed boost is just a factor.
Thanks for the responce but i'm still fairly new with this, I don't know what you mean by the xxx = parts
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_$$anonymous$$iles_Per_Hour_To_$$anonymous$$eters_Per_Second(topSpeed);
This will bring the syntax error unknown identifier 'value'. I believe this is due to changing value to speed.
Answer by tanoshimi · Nov 19, 2013 at 12:13 PM
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?
$$anonymous$$y 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.
Answer by jheiling · Nov 19, 2013 at 12:42 PM
You exit the method in line 12 with return value 0.44704;*, so the following if(speedboost)... will never execute. Change the method to
if(speedboost)
{
return value * 2;
}
else
{
return value * 0.44704;
}
and it will work.
Sorry, seems like code formatting isn't working for me...
fixed it ;)
btw that doesn't make sense either ;) The function should convert from $$anonymous$$PH 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 ;)
Thanks for your response but it didn't increase the speed.
Actually the method is just calculating the speed... you still have to apply it somehow.
Answer by undead-steve · Nov 19, 2013 at 10:33 PM
Rather than bothering with a boolean, why not just make it a float? Then you can do
speedboost =1f;
if(other.gameObject.tag == "SpeedBoost")
{
speedboost = 2f;
Debug.Log("Hit");
}
and
function ConvertMPH2MPS(speed : float) : float
{
return speed * 0.44704 * speedboost;
}
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'
Your answer
Follow this Question
Related Questions
onEnterCollider returns Collider 1 Answer
OnTriggerEnter or If statement problem 2 Answers
if statement for 6 ints help C# 2 Answers
Secound Animator.GetCurrentAnimatorStateInfo(0).IsName() doesn't work in if Statement 0 Answers
Time is not stopping 2 Answers