- Home /
How can i rotate this vector over time?
This is the first time I've rotated objects in this manner, and I'm wondering how to add an "over time" component to what i already have. Basically I Click the object and it rotates instantly, and I want it to happen over period of like 0.5 seconds for polish purposes.
if(hit.collider.tag == "Pipe") {
zRot -= 90;
hit.transform.eulerAngles = Vector3(xRot, yRot, zRot);
}
I've moved objects before using the code below, and am wondering if a similar method would be used for rotating.
function Update () {
endPoint = target.transform.position;
transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
Answer by fifthknotch · Mar 11, 2014 at 04:22 AM
The best method to get something to rotate the same amount over time is to use Time.deltaTime. This will force the object to keep its rotation frame rate independent:
http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html
Use transform.Rotate to actually rotate the object. More on that can be found:
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
And here is a code I made. Fill in the variables to suit your game best. Click to start rotation. You can repeatedly click to keep the rotation going.
#pragma strict
var x : int;
var y : int;
var z : int;
var maxRotateTime : float;
private var rotating : boolean = false;
private var counter : float;
function Update () {
if (Input.GetMouseButtonDown(0) && !rotating) {
rotating = true;
}
if (rotating) {
counter += Time.deltaTime;
if (counter < maxRotateTime)
transform.Rotate(Vector3(x, y, z) * Time.deltaTime);
else {
rotating = false;
counter = 0;
}
}
}
Answer by DiligentGear · Mar 11, 2014 at 04:24 AM
Unfortunately, this might need you to dig into Coroutine. The official tutorial is quite informative:
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
bool bIsRunning = false; // Check if this coroutine is already running
Vector3 rotateAngle = new Vector3 (0f,0f,-90f); // Corresponding euler angle
float rotateSpeed = 5f; // How fast you want object to rotate
IEnumerator RotateByClick(RaycastHit hit,Vector3 toEuler,float rotateSpeed)
{
if (!bIsRunning)
{
bIsRunning = true;
Quaternion fromQua = hit.transform.rotation;
Quaternion toQua = Quaternion.Euler(toEuler);
// 'Translate' euler angle to a quaternion
float deltaT = 0f;
while (deltaT < 0.99f)
// The rotation is completed when deltaT reaches 1,
// you can adjust the fraction if it's not accurate enough.
{
hit.transform.rotation = Quaternion.Slerp(fromQua,toQua,deltaT);
// This is the rotation version of Lerp
deltaT += Time.deltaTime * rotateSpeed;
yield return null;
}
bIsRunning = false;
}
yield return null;
}
// Then you can add this to your raycast function
if (hit.collider.tag == "Pipe")
{
StartCoroutine(RotateByClick(hit,rotateAngle,rotateSpeed));
}
Why unfortunately? This sounds like coroutines are worst thing ever, while they're quite important for Unity program$$anonymous$$g.
Btw - I also suggest reading Unity Gems article on coroutines.
Apparently he is a beginner and he might have trouble understanding the syntax and how it works.
And I never meant coroutines are bad. I just meant that he might learn something that overwhelms a beginner.
I'm also doing it in Unity JS so coroutines aren't needed i believe? Ins$$anonymous$$d using While(???)
@DiligentGear - I believe after reading linked tutorials, he should be fine :)
@ImPvEspec - coroutines are language independent, so you can use them in JS as well.
You can of course use while, but calling it in Update will make it execute in current frame. So if you change some position, rotation etc. inside while, only last result will be relevant. That's why coroutines were introduced - they work in a different way, and can be executed across more than one frame. I really suggest reading through provided links, and experimenting with coroutines a bit.
You cannot have a coroutine inside that update function. It produces and error. :)
Your answer
Follow this Question
Related Questions
How to limit the range of rotation of the world around my player. 0 Answers
Object's vectors are not moving with the object, what did i do wrong? 1 Answer
Add a Vector3 to the rotation of an obj and have it behave with logic! 0 Answers
Turret rotation on one axis problems 2 Answers
Euler Angles and Vectors 3 Answers