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 Kuro Okami · Nov 20, 2014 at 06:44 AM · upside-downget-time

Flipping character after set time upside-down

Ok, so I'm trying to put a feature into my game which automatically flips the character after a period (2-5 seconds) of them being upside down, I'm guessing that I'd have to use coroutine, but I'm not sure how. And I'm also not sure how to get the script to check if the character is upside down at the time. If anyone has an annotated script which I can try to understand and learn from that'd be great also.

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 HarshadK · Nov 20, 2014 at 07:22 AM

Rather than coroutines you can use InvokeRepeating if you want to flip character after a fixed interval of time. Just use a boolean that you set as per the state of the character which you can check to decide.

 bool isUpsideDown = false; // We consider he starts straight up.
 
 void Start()
 {
     InvokeRepeating("FlipCharacter", 5.0f);  // 5.0f can be replaced with any time duration you want
 }
 
 void FlipCharacter()
 {
     if(isUpsideDown) {
         // Rotate the character to be straight up. You can simply change its rotation or can apply smoothing to flip and all that stuff.
         isUpsideDown = false; // Set bool to set the current state of character
     } else {
         // Rotate the character to be upside down. Again you can do it simply or use fancy stuff
         isUpsideDown = true; // Set bool to set the current state of character
     }
 }

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 Kuro Okami · Nov 21, 2014 at 07:26 AM 0
Share

Ok, so I tried that word for word but it gives me a "No overload for method 'InvokeRepeating' takes '2' arguments" error on the invoke repeating line, what happened and what do I do? Also would I be able to set it so it only starts the timer if it is at a specific angle or range? I have it so my character can go on ramps and angles.

avatar image HarshadK · Nov 21, 2014 at 07:32 AM 0
Share

Yea, sorry. InvokeRepeating takes three arguments so that line should be:

 InvokeRepeating("FlipCharacter", 5.0f, 5.0f);

Yes, you can invoke the function as and when required but for that you need to invoke the function each time using Invoke. Something like:

  void FlipCharacter()
  {
      if(isUpsideDown) {
          // Rotate the character to be straight up. You can simply change its rotation or can apply smoothing to flip and all that stuff.
          isUpsideDown = false; // Set bool to set the current state of character
          Invoke("FlipCharacter", 5.0f)  //Set this function to invoke after set amount of time
      } else {
          // Rotate the character to be upside down. Again you can do it simply or use fancy stuff
          isUpsideDown = true; // Set bool to set the current state of character
          Invoke("FlipCharacter", 5.0f)  //Set this function to invoke after set amount of time
      }
  }

Similarly you can even set it to trigger anytime you want under any condition you want to check like slopes.

avatar image Kuro Okami · Nov 21, 2014 at 08:53 AM 0
Share

Thanks, one last thing, how would I get the rotation or say, if the rotation is less than 280 but more than 260?

avatar image HarshadK · Nov 21, 2014 at 08:57 AM 0
Share

If you are checking the rotation around any axis say in this case around x axis, you do it like:

 if(transform.rotation.eulerAngles.x > 260 && transform.rotation.eulerAngles.x < 280)
 {
     // Do your thing here
 }
avatar image Kuro Okami · Nov 21, 2014 at 09:25 AM 0
Share

Thanks for the help

Show more comments
avatar image
0

Answer by Uldeim · Nov 20, 2014 at 07:36 AM

When your character gets flipped, use StartCoroutine("FlipTimer");

If, at some point before it's done, the character gets unflipped, you can then use StopCoroutine("FlipTimer");

Because you're using the version of StartCoroutine with a string argument, you can then stop the routine before it completes. In the coroutine itself, you'll want to yield a WaitForSeconds and check at the very end that the character is flipped (timing issues are a pain, so always double-check). eg.

 IEnumerator FlipTimer()
 {
    yield return new WaitForSeconds(5.0f);
    if (character.isFlipped)
    {
       UnflipCharacter();
    }
 }
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

27 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

Related Questions

rotate player on y axis or Vector.up regradless of orientation 0 Answers

Why is my scene upside down 1 Answer

Can I customise CharacterController Physics Module ? 0 Answers

My weapon just... turns upside down 1 Answer

How to check if an object is upside-down? 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