Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LeftyTwoGuns · Nov 03, 2015 at 03:21 AM · rigidbody2dcomponentwaitforsecondsienumerator

How to disable and re-enable components in the same function?

Possibly not the best way to ask my question, which is pretty simple but I can't figure out how to do it

I have a custom 2D controller on a player sprite that uses its own forces, however I'd like to disable this motor and use Unity physics to perform a knockback when it takes damage, like so:

 public void DoKnockback()
     {
         motor.enabled = false;
         rb.AddForce(new Vector3(-1, 1, 0) * 200);
         rb.isKinematic = false;
     }

But then after that's done I need to immediately re-enable the motor and isKinematic or else the character will fall through the scenery. I was thinking WaitForSeconds but that's only useable in an IEnumerator and I couldn't for the life of me figure out how to do this in a coroutine. Any suggestions?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Inok · Nov 03, 2015 at 03:57 PM

If you want force affect body you need wait some time before you reenable kinematic body:

 IEnumerator DoKnockback()
 {
   motor.enabled = false;
   rb.isKinematic = false;
   rb.AddForce(new Vector3(-1, 1, 0) * 200 , ForceMode.Impulse); // impulse mode optional if you want to push more quickly in less time.
   yield return new WaitForSeconds(time); // here you set delay to give force do it job. If you not give delay and instantly reenable kinematic body it stop move by physics before it achieve all force. Delay depend on Force mode that you set (for "impulse" you can set lower delay, for "force" - higher).
   rb.isKinematic = true;
   motor.enabled = true;
 }
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 LeftyTwoGuns · Nov 05, 2015 at 02:38 AM 0
Share

Thank you! Works perfectly. I'm glad you pointed out the the custom controller taking over the Unity physics when re-enabling it, that completely slipped my $$anonymous$$d. It's a very smooth transition now. Thanks again

avatar image
0

Answer by knudsjef · Nov 03, 2015 at 04:18 AM

Just disable it and re enable it by motor.enabled = true and rb.isKinematic = true. I don't think it should be a problem.

Comment
Add comment · Show 2 · 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 LeftyTwoGuns · Nov 03, 2015 at 05:32 AM 0
Share

But I was under the impression that lines in a function don't execute sequentially

avatar image knudsjef LeftyTwoGuns · Nov 03, 2015 at 02:46 PM 0
Share

Yep, they do :)

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

34 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 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

Instantiate an object as soon as another object is destroyed 2 Answers

fps calculation with while loop in coroutine 2 Answers

IEnumerator Stalling Or Stopping 2 Answers

Enable collider then disable it after a certain amount of time 1 Answer

yield return new WaitForSeconds () not working 1 Answer


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