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 BrimTheOne · Jul 31, 2013 at 02:30 PM · c#timenewbieseconds

C# Make something happen for X amount of Seconds.

Hello, the title says almost all there is. I would like to know how i would do something like this in c#. if(input.GetKeyDown("2")){

 spin();
 
 }
 
 
 spin()
 {
 //how to make this happen for 5 seconds, even after the key is //up?
 transform.rotation.z - 1;
 //and then how do i make it return to original rotation?
 
 }

Regards Brim

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 Peter G · Jul 31, 2013 at 02:36 PM 0
Share

I think this answer will help you. You should look into using coroutines.

2 Replies

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

Answer by Peter G · Jul 31, 2013 at 04:27 PM

You should probably use a coroutine:

 class RotateClass : MonoBehaviour{
 
     public float turnSpeed = 90f;
     //turn speed in degrees.
     public float turnTime = 5f;
     //how long should we turn for.
 
     private bool spinning = false;
     //are we currently spinning?
     //we use this to prevent multiple instance of spinning starting.
  
     void Update(){
         if (input.GetKeyDown("2") && !spinning)
             StartCoroutine( Spin() );
     }
  
     IEnumerator Spin () {
         spinning = true;
         var time = 0;
         var turnIncrement = new Vector3( 0 , 0 , turnSpeed * Time.deltaTime);
         //Turn towards the side.
         while ( time < turnTime ) {
               time += Time.deltaTime;
               transform.rotation.eulerAngles += turnIncrement;
               yield return null;
          }
         //Turn back to the starting position.
         while ( time > 0 ) {
               time -= Time.deltaTime;
               transform.rotation.eulerAngles -= turnIncrement;
               yield return null;
          }
          spinning = false;
     }
 }
Comment
Add comment · Show 9 · 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 Xtro · Jul 31, 2013 at 04:31 PM 0
Share

Yield co-routines become a headache. They make the code very complex even for the simple solutions. I don't use them and I don't suggest the beginners to use them.

avatar image perchik · Jul 31, 2013 at 04:35 PM 0
Share

The fact you don't use them does't mean they aren't the right way to do things. Doing it on update requires every such time delayed function to have a flag and a separate function. Coroutines are definitely the right answer

avatar image Xtro · Jul 31, 2013 at 04:42 PM 0
Share

Storing the keydown time and testing it against the current time is way simpler to write and to be understood by anyone else. Just look at the code length of your code and $$anonymous$$e.

avatar image perchik · Jul 31, 2013 at 04:45 PM 0
Share

Right. It works fine if it only needs to spin. What if you want it to spin when you press one key, and jump when you press the next, but want it simultaneously. Checking on Update is a fine way to do it if you never want to extend it.

Also the length of code could be made exactly the same, @Peter G just polished the rotation code.

avatar image Xtro · Jul 31, 2013 at 04:56 PM 0
Share

Oh sorry, I thought PeterG was commenting with me. Implementing different behaviors for different keys is not the topic of this question. And I'm sure that behavior is also possible by storing the event times ins$$anonymous$$d of using yield methods. I won't think about it since it's not the current question.

Show more comments
avatar image
2

Answer by Xtro · Jul 31, 2013 at 02:44 PM

1) Store the time when key down

2) Don't put the Spin call in the keydown check (if)

3) Before spin call, test the current time against stored key down time + 5

4) if you pass the test, run your work.

Should be something like that:

 class YourClass : MonoBehaviour{
     float KeyDownTime = float.MinValue;
     Quaternion OriginalRotation;
 
     void Start(){
         OriginalRotation = transform.rotation;
     }
 
     void Update(){
         if (input.GetKeyDown("2")) KeyDownTime = Time.time;
 
         if (Time.time < KeyDownTime + 5){
             Spin();
         }
         else transform.rotation = OriginalRotation;
     }
 
     void Spin(){
         var R = transform.rotation;
         R.z--;
         transform.rotation = R;

         // in javascript we could do only 1 line...
         // transform.rotation.z--;
         // instead of 3 lines of C# code
         // but in C# we have to use a temp R value.
     }
 }
Comment
Add comment · Show 7 · 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 BrimTheOne · Jul 31, 2013 at 03:08 PM 0
Share

this just gives me error at the "transform.rotation.z - 1;" That "Only assignment, call, increment, decrement, and new object expressions can be used as a statement"

Also, when i remove it and just test it with debug.log it bugs out when i start the game :S

avatar image Xtro · Jul 31, 2013 at 03:56 PM 0
Share

I just copied your line into Spin method. You should do the syntax fixing according to your logic.

What bug do you see when you run it ? (I didn't test the code on my unity. I just wrote a template for you to understand)

avatar image Peter G · Jul 31, 2013 at 04:30 PM 0
Share

This line is totally wrong:

  transform.rotation.z - 1;

  1. It's an expression not a statement. That's what's giving him the error.

  2. You can't assign a value like that in C#. You have to copy the entire quaternion to a temporary variable, change the value that you want, then reassign it to transform.rotation.

  3. I think the OP means to edit the euler angles.

avatar image Xtro · Jul 31, 2013 at 04:32 PM 0
Share

Yes, I know where the error is. Like I said I just copied his line of code assu$$anonymous$$g he can fix that line. Anyway, I'ma edit my answer for a better testing.

avatar image HansMcCode · Jul 31, 2013 at 05:19 PM 1
Share

The only thing with this is the item will spin faster or slower due to the machine its being run on wont it? Since you aren't using Time.deltaTime to standardize the rotation? I would personally go with a IEnumerator, the concept might be a little hard to grasp if you're new to coding but once figured out they are incredibly helpful. This seems like a good simple practice situation to learn how to use them.

Show more comments

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Find time value between two other time values? C# 2 Answers

How to double jump 1 Answer

Pause game while GUI instructions displayed? 1 Answer

Pause Game When Function Is Enabled? 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