Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Andyy G · Nov 19, 2013 at 12:10 PM · ontriggerenterif-statementsreturn

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;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andyy G · Nov 19, 2013 at 01:00 PM 0
Share

I feel I should add, this is part of the car tutorial scripts from the unity store that I am working with.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andyy G · Nov 19, 2013 at 12:59 PM 0
Share

Thanks for the responce but i'm still fairly new with this, I don't know what you mean by the xxx = parts

avatar image Bunny83 · Nov 19, 2013 at 01:08 PM 0
Share

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).

avatar image Andyy G · Nov 19, 2013 at 01:12 PM 0
Share

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.

avatar image Bunny83 · Nov 19, 2013 at 10:29 PM 0
Share

ohh, yes, sorry :D i will fix this...

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andyy G · Nov 19, 2013 at 12:21 PM 0
Share

$$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.

avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jheiling · Nov 19, 2013 at 12:43 PM 0
Share

Sorry, seems like code formatting isn't working for me...

avatar image Bunny83 · Nov 19, 2013 at 12:51 PM 1
Share

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 ;)

avatar image jheiling · Nov 19, 2013 at 12:56 PM 0
Share

cheers! you're probably right...

avatar image Andyy G · Nov 19, 2013 at 12:57 PM 0
Share

Thanks for your response but it didn't increase the speed.

avatar image jheiling · Nov 19, 2013 at 01:01 PM 1
Share

Actually the method is just calculating the speed... you still have to apply it somehow.

Show more comments
avatar image
0

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'

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges