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 Bondolero · Apr 27, 2016 at 03:19 PM · rotaterotate objectz-axisincrease

How to smoothly increase the speed of rotation

Hello Devs!

I want my object to increase rotation speed smoothly from 0 speed to 5 on the z axis, so if someone knows the solution i would be very grateful if you could share it with me!

Here is my code:

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;

public class rotateleft : MonoBehaviour, IEventSystemHandler {

 public float speed = 2f;
 bool buttonHeld = false;
 
 public void pressed (BaseEventData eventData)
 {
     buttonHeld = true;
     GameObject.Find("LeftImage").GetComponent<Animation>().Play("LEFT");
 }
 public void notpressed(BaseEventData eventData)
 {
     buttonHeld = false;
     GameObject.Find("LeftImage").GetComponent<Animation>().Play("LEFT1");


 }

 public void FixedUpdate()
 {
     if (buttonHeld)
     {
         transform.Rotate(Time.smoothDeltaTime, 0, speed * Time.deltaTime);

     } 

         
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Apr 27, 2016 at 03:48 PM

 public float speed = 1f;
 public float speed_max = 1f;
 public float speed_increaseSpeed = 2f;
 
 private IEnumerator IncreaseSpeed() {
      for(float t = 0; t < 1f; t += Time.deltaTime * speed_increaseSpeed ) {
             speed = Mathf.Lerp(speed, speed_max , t);
            yield return null;
      }
 }
Comment
Add comment · Show 3 · 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 Bondolero · Apr 27, 2016 at 04:08 PM 0
Share

Thank you for reply and now i have changed my code:

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;

public class rotateleft : $$anonymous$$onoBehaviour, IEventSystemHandler {

 bool buttonHeld = false;

 public float speed = 1f;
 public float speed_max = 1f;
 public float speed_increaseSpeed = 2f;
 
 public void pressed (BaseEventData eventData)
 {
     buttonHeld = true;
     GameObject.Find("LeftImage").GetComponent<Animation>().Play("LEFT");
 }
 public void notpressed(BaseEventData eventData)
 {
     buttonHeld = false;
     GameObject.Find("LeftImage").GetComponent<Animation>().Play("LEFT1");


 }

 public void FixedUpdate()
 {
     if (buttonHeld)
     {

         StartCoroutine (IncreaseSpeed ());

     } 

         
 }

 private IEnumerator IncreaseSpeed() {
     for(float t = 0; t < 1f; t += Time.deltaTime * speed_increaseSpeed ) {
         speed = $$anonymous$$athf.Lerp(speed, speed_max , t);
         transform.Rotate(0, 0, speed);
         yield return null;
     }

} }

but now it's rotating very fast when i hold the button, why is this happening?

avatar image Bondolero Bondolero · Apr 27, 2016 at 04:38 PM 0
Share

never $$anonymous$$d, got it working now i had set speed of 200 in the inspector.

avatar image Cherno Bondolero · Apr 27, 2016 at 05:24 PM 0
Share

Good. I converted my comment to an answer. Feel free to accept it to help others stumbling upon this thread :)

Note that if you want to increase speed without a limit (bad idea but what the hell!), you can try something like this:

  public float speedIncreaseFactor = 2f;
 
 private IEnumerator IncreaseSpeed() {
     while(true) {
          speed += Time.deltaTime * speedIncreaseFactor;
          transform.Rotate(0, 0, speed);
          yield return null;
      }

You will have to use StopCoroutine in this case when you let go of the button or it won't stop :) If you want to use StopCoroutine, you have to use the alternate way of StartCoroutine which takes the routine's name as a string paremeter.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i rotate a object on z axis with two fingers? (Mobile) 0 Answers

Plane Control Script rotating unexpectedly 1 Answer

2D game look at mouse rotate 1 Answer

2d - Follow cursor by having object rotate around z-axis 1 Answer

In FBX Export from Maya do I select Retain Quaternion Interpolation? 0 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