- Home /
How can i rotate object smooth without stop ?
I'm using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinObject : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
StartCoroutine(ContinuousRotation());
}
IEnumerator ContinuousRotation()
{
while (true)
{
transform.Rotate(Vector3.forward, 10);
yield return new WaitForSeconds(5f);
}
}
}
It's rotating the object but the problem is no matter what time i'm setting when running the game the rotating speed is the same. I tried: yield return new WaitForSeconds(1f); then 5f in the original it was 0.01f but the speed never change.
Second problem is that the rotating making the object some flickering not flickering but some interruption to the eyes it's not really rotating smooth. Maybe the problem is the speed ?
Answer by Acid_kenobi · Jul 21, 2017 at 10:40 PM
Keep it simple?
void Update()
{
transform.rotation += Vector3.forward * time.deltatime * speed;
}
Sorry for crap formatting, on my phone. Good luck
@Hellium Actually the OP doesn't say they want a delay at all, not sure where you've pulled that from? They simply state that they have tried different values in an attempt to alter the speed of the rotation.
Downvote all you like champ, your answer was bloated and will likely only confuse someone starting out.
Then, explain me why the OP would want to use a coroutine with a yield return new WaitForSeconds(5f)
? Rotating an object in the Update
function is the 13th tutorial of Unity, coroutine is the 14th of the Intermediate level. I think the OP would have used a similar script as yours if he wanted a simple rotation without a wait.
But, I may be wrong, and using a coroutine is completely irrelevant if the OP didn't want a wait between rotations.
Answer by Hellium · Jul 21, 2017 at 10:09 PM
You have to start the coroutine inside the Start function, not the Update one. Otherwise, every frame, you will run a coroutine. Thus, you will have hundreds of coroutines trying to rotate your object.
Moreover, I think you don't quite understand how coroutines work. Take this code :
void Start () {
StartCoroutine(ContinuousRotation());
}
IEnumerator ContinuousRotation()
{
float duration = 10f ;
float speed = 1 ;
WaitForSeconds wait = new WaitForSeconds(5f);
while (true)
{
for( float time = 0 ; time < duration ; time += Time.deltaTime )
{
transform.Rotate(Vector3.forward, speed * Time.deltaTime);
yield return null ;
}
yield return wait;
}
}
Huh? who downvoted this answer? It's absolutely correct. It's the exact reason for the behaviour the OP as observed. Each frame in Update a new Coroutine will bestarted That's why the object rotates with a more or less constant (frame based) speed (10 degree every frame). Further more once the delay time in the first coroutine has expired the rotation speed will double. After another delay cycle the rotation speed would be trippled and so on...
The example given might not be exactly what the OP wants but Unity Answer is not there so other people write code for you but to help you with your problems. The main problem here is that the OP stars a new infinite coroutine every frame.
ps: If you downvote a post always leave a comment with a reason. Also keep in $$anonymous$$d that downvotes should be used sparsely. It's not a "like / dislike" system. It's all about correctness. Downvotes usually indicate misleading or wrong information. The coroutine in this answer was not announced as a solution but as an example for how to use coroutines.
The question isn't super clear what's the actual goal so no one answering can claim he has the right solution, that's up to the OP.
Your answer
Follow this Question
Related Questions
Why when using LookAt to make the turret facing the target it's facing the other direction ? 1 Answer
Why the cannon is never rotate looking at the target ? 1 Answer
How can i keep the transform height while it's moving ? 1 Answer
How can i change slider of Range in Update method while the game is running ? 0 Answers
How can I make two new methods one for the scaling one for the rotating to use from other scripts ? 2 Answers