Hi. I am trying to use have an object rotate from (0,0,0) to (0,0,90) in two seconds. When I try this bit of code, I get the following error:
‘UnityEngine.Vector3’ to
‘UnityEngine.Transform’.
What am I missing? Thanks
var from : Transform;
var to : Transform;
var speed = 0.1;
function Update () {
transform.rotation =
Quaternion.Lerp (from.rotation, to.rotation, Time.time * speed);
to =transform.eulerAngles = Vector3(0, 0, 0);
from=transform.eulerAngles = Vector3(0, 0, 90);
}
You’re assigning transform.eulerAngles (a Vector3) to a Transform (to and from) - that’s what’s causing the error. In order to rotate at constant speed, the best solution is to use Slerp in a coroutine, like this (script attached to the rotating object):
var rotating = false;
function RotateZ(angle: float, duration: float){
// don't start another rotation while the previous has not ended
if (rotating) return;
rotating = true; // set the flag to block other RotateZ calls
var oldRot = transform.rotation;
// calculate the new rotation - current rotation * desired rotation:
var newRot = Quaternion.Euler(0, 0, angle) * oldRot;
var t: float = 0; // t is the control variable
while (t < 1){
t += Time.deltaTime/duration;
transform.rotation = Quaternion.Slerp(oldRot, newRot, t);
yield; // return and resume here next frame
}
rotating = false; // rotation ended;
}
Call the function RotateZ(90, 2) and the object will rotate 90 degrees around Z in exactly 2 seconds.
Try:
var from : Transform;
var to : Transform;
function Start () {
to.transform.eulerAngles = Vector3(0, 0, 0);
from.transform.eulerAngles = Vector3(0, 0, 90);
}
function Update () {
var speed = 0.1;
transform.rotation =
Quaternion.Lerp (from.rotation, to.rotation, Time.time * speed);
}
The error was with your assignment of to and from as seen in the start()