- Home /
Dividing the circular rotation into parts?
Hi all,
I have a requirement that a Game Object should rotate in clockwise circular motion and divide the whole circular path into 6 parts and Randomize the movement speed in each part. Please suggest me ideas to do this.
Thanks, Hema
Answer by hema · Mar 14, 2014 at 09:51 AM
Have found a way using Quaternion.Euler. Below is the code: m_Rotation += 1.0f; transform.rotation = Quaternion.Euler(new Vector3(0,0,-m_Rotation * m_Speed)); if(m_Rotation == 360) { m_Rotation = 0; } if(m_Rotation < 60) { m_Speed = 1; } if(m_Rotation > 60 && m_Rotation < 120 ) { m_Speed = 2; } But dint get a smooth transition. Help me to improve the answer.
What you want is the Lerp function. To use it in your code change.
transform.rotation = Quaternion.Euler(new Vector3(0,0,-m_Rotation * m_Speed));
to
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(0,0,-m_Rotation * m_Speed)), Time.deltaTime * 5);
You can adjust the smooth rate by changing Time.deltaTime 5 to Time.deltaTime whatever you want. Higher numbers will of course make it move faster.
Also the first post I made has many typos's as I was hastily typing it out just in the unity answers text box and did not test it in unity or monodevelop but the logic used in it is sound. I am going to delete it though as it is not very helpful in it's current state.
Answer by jaapflonk · Mar 13, 2014 at 10:06 AM
I dont understand your question, but this might work:
using UnityEngine; using System.Collections;
public class example : MonoBehaviour {
public float randomNumber;
public float timer = 0f;
void Start() {
randomNumber = Random.Range(1, 10);
}
void Update() {
timer += Time.deltaTime * 1;
print(timer);
if (timer > randomNumber) {
Rotate();
randomNumber = Random.Range(1, 10);
timer = 0;
}
}
void Rotate() {
transform.Rotate(Vector3.left * 60);
}
}
Every 0 to 10 seconds it will rotate 60 degrees
The Game object will be moving in a circular motion. One full rotation should be divided into 6 parts. Each part should have different speed.
Usually one rotation takes 90 degrees it seems from the document so need different speed in each 15 degrees rotation.
Your answer
Follow this Question
Related Questions
Different speed on different phones 0 Answers
Instant Rotation 2 Answers
Decreasing object rotation speed until it stops? 0 Answers
Turret: Getting a constant rotation speed 2 Answers
Rotate an object in a specific time 0 Answers