- Home /
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
By the way your second if is wrong I think. It should be ==
@fafase Oh yeah, I hadn't actually tested it yet, because I couldn't test with errors :P Thanks
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.
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.
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);
I did try impulse and it just made it go crazy. Okay, I'll try that :)
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
Follow this Question
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