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
1
Question by jpcoder · Jan 13, 2014 at 03:48 PM · 2dwheelspin

2D wheel spin

First, this is a 2D game. I need to spin a sprite a set amount of degrees and then have it slowly stop with a little bounce back easing (like a roulette wheel). I need it to spin the same amount (degrees) and take the same amount of time on each spin. Anyone have some code to do this?

Thanks...

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
3
Best Answer

Answer by robertbu · Jan 13, 2014 at 04:39 PM

Given what you are trying to achieve, my first thought is an AnimationCurve. The AnimationCurve class allows you to edit a curve that you can then apply in your calculations. Here is a bit of code to get you started:

 #pragma strict
 
 var ac : AnimationCurve;
 private var spinning = false;
 
 function DoSpin(time : float, maxAngle : float) {
     spinning = true;
     var timer = 0.0;
     var startAngle = transform.eulerAngles.z;
 
     while (timer < time) {
         var angle = ac.Evaluate(timer/time) * maxAngle;
         transform.eulerAngles = Vector3(0.0, 0.0, angle + startAngle); 
         timer += Time.deltaTime;
         yield;
     }
     transform.eulerAngles = Vector3(0.0, 0.0, maxAngle + startAngle); 
     spinning = false;
 }
 
 function Update() {
     if (Input.GetKeyDown(KeyCode.Space) && !spinning) {
         DoSpin(3.0, 270.0);
     }
 }

Note once this script is attached, you can click on the 'ac' variable and edit the curve. And here is a shot of the animation Curve.

alt text

Note that the end point of the curve should be as close to (1.0, 1.0) as you can make it. Also note the high point of the graph is a above 1.0. This combination will cause the spinner to go past the target degrees and then return.


curve2.png (21.5 kB)
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 HappyMoo · Jan 13, 2014 at 04:58 PM 0
Share

Nice usage of AnimationCurves +1

avatar image jpcoder · Jan 13, 2014 at 05:08 PM 0
Share

I think this may be exactly what I need. I'll give it a try tonight. Thanks.

avatar image jpcoder · Jan 13, 2014 at 05:08 PM 0
Share

Also, didn't know you could do that with the AnimationCurve. Very cool.

avatar image jpcoder · Jan 13, 2014 at 06:33 PM 0
Share

Just tried it. That was perfect! Had to play with the parameters a bit but it looks good. Thanks!

avatar image iVaP0R · Feb 28, 2015 at 08:50 PM 1
Share

For the people who need this in C#

      public AnimationCurve ac;
      private bool spinning = false;
      private Transform startAngle;
 
     void doSpin(float time, float maxAngle)
      {
          spinning = true;
          float timer = 0.0f;
          float startAngle = transform.eulerAngles.z;
 
         while(timer < time)
         {
             float angle = ac.Evaluate(timer / time) * maxAngle;
             transform.eulerAngles = new Vector3(0.0f, 0.0f, angle + startAngle);
             timer += Time.deltaTime;
            
         }
 
         transform.eulerAngles = new Vector3(0.0f, 0.0f, maxAngle + startAngle);
               spinning = false;
      }
  
  
  void  Update ()
  {
      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && !spinning) 
      {
          doSpin(3.0f, 270.0f);
      }
  }
Show more comments
avatar image
0

Answer by kbhatt · Mar 17, 2015 at 03:40 PM

public IEnumerator DoSpin(float time, float maxAngle) { DateTime startSpinTime = System.DateTime.Now; spinning = true; float timer = 0.0f; float startAngle = wheel.GetComponentInChildren().eulerAngles.z;

 while (timer < time) 
 {
     float currentTime = (float)(System.DateTime.Now - startSpinTime).TotalSeconds;
     float acceleration = 1 * currentTime;
         float angle = ac.Evaluate(timer/time) * maxAngle;

     wheel.GetComponentInChildren<RectTransform>().eulerAngles = new Vector3(0.0f, 0.0f, -(angle + startAngle));

     timer += (Time.deltaTime*acceleration);

     yield return 0;
 }

 wheel.GetComponentInChildren<RectTransform>().eulerAngles = new Vector3(0.0f, 0.0f, -(maxAngle + startAngle));
 spinning = false;

}

This will also allow you to accelerate and decelerate based on defined 'acceleration' This code spins anti clockwise so just subtract 360f to whatever angle you want to stop. Thanks to main robertbu for the important bit above.

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

23 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

Related Questions

Spin a 2D object with mouse drag 2 Answers

2D Animation does not start 1 Answer

Control individual wheels of a top down 2d vehicle 0 Answers

2D Joint/Wheel Question 1 Answer

Adjusting tire smoke particle emitter based on wheel slip 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