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 EvilSquid · Aug 17, 2013 at 09:50 AM · c#functionstopvoidexecution

How can I stop executing a Function in a void Function?

Hi,

I have a function which I'm using to create a realistic take off of an airplane, and it involves a small torque making the planes nose pitch up. I have put the actual force into a function named TakeOff(), and I have put in FixedUpdate() a code that executes TakeOff().

 void FixedUpdate () 
     {
                 
         if(rigidbody.velocity.magnitude > TakeOffSpeed)
             TakeOff();
         if(rigidbody.velocity.magnitude = TakeOffSpeed + 50)
             //Stop Executing TakeOff()
             
         
         
     }
     
      void TakeOff()
     {
             rigidbody.AddForce (0,50,0,ForceMode.Acceleration);
         rigidbody.AddRelativeTorque (20,0,0,ForceMode.Force);
     }

The problem I have is that because it is a void Function I can't use return, so I don't know if there is another way around it, or if I have to set the Functions to a different type, and I don't know what type!

Thanks

Comment
Add comment · Show 3
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 YoungDeveloper · Aug 17, 2013 at 10:31 AM 0
Share

Actually, you can.

 return;
avatar image fafase · Aug 17, 2013 at 10:35 AM 1
Share

By the way your second if is wrong I think. It should be ==

avatar image EvilSquid · Aug 17, 2013 at 10:51 AM 0
Share

@fafase Oh yeah, I hadn't actually tested it yet, because I couldn't test with errors :P Thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Doeko · Aug 17, 2013 at 10:26 AM

Just change your logic so that TakeOff() is only called from FixedUpdate()when your condition is met. There is no need to "stop" it as TakeOff is not a self-repeating function.

You can also use return keyword in a void but I don't know what good it would do in this case.

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 EvilSquid · Aug 17, 2013 at 10:53 AM 0
Share

But if it's a force and I only want the force to last until my variable +50 is met, how can I stop the force?

It's not self repeating but the force that I'm applying continues infinitely.

avatar image Doeko · Aug 17, 2013 at 10:58 AM 0
Share

If you don't want the force to be continuous use Force$$anonymous$$ode.Impulse ins$$anonymous$$d. Otherwise (I'm not an expert on the physics engine so this is hypothetical) I imagine you could do something like:

 rigidbody.AddForce(0, -50, 0, Force$$anonymous$$ode.Acceleration);
avatar image EvilSquid · Aug 17, 2013 at 10:59 AM 0
Share

I did try impulse and it just made it go crazy. Okay, I'll try that :)

avatar image Doeko · Aug 17, 2013 at 11:01 AM 0
Share

If you apply an impulse of 50 every frame it's possible it goes crazy. You need to play with the values to get a feel of what works for your object.

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

17 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

Related Questions

C# Return Type Error? 1 Answer

start called directly after instantiate? 1 Answer

How to make a List or Array of functions with type Void 4 Answers

Sending a message from javascript to C# 1 Answer

Check if a function is no longer being called? 3 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