- Home /
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);
}
}
}
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;
}
}
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?
never $$anonymous$$d, got it working now i had set speed of 200 in the inspector.
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
Follow this Question
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