My task is to rotate and move an object into a new position and rotation in a given time. Say I have to move a cube from (0,0,0)|(0,0,0) to (1,1,1)|(0,90,0) in 5 seconds. Could anyone explain to me please how to achieve that?
My experiment is:
private IEnumerator DoMove(/*float time, Vector3 targetPosition*/)
{
foreach(WayPoints wp in tposes)
{
float counter = 0;
float duration = wp.timeStamp;
while (counter < duration)
{
counter += Time.deltaTime;
Vector3 curpos = cube.transform.position;
Quaternion crot = cube.transform.rotation;
float time = Vector3.Distance(curpos, wp.position.position) / (duration - counter) * Time.deltaTime;
cube.transform.position = Vector3.MoveTowards(curpos, wp.position.position, time);
cube.transform.rotation = Quaternion.Lerp(crot, wp.position.rotation, time);
yield return null;
}
Debug.Log("Reached position: " + wp.position.position + " in time: " + Time.time);
}
}
The cube gets to its position in time, but rotation is late. The end position of the cube should be (2,2,2)|(0,120,0), but it stops moving at (2,2,2)|(0,~110,0).
There are two positions I want the cube to be in:
(1,1,1)|(0,90,0)in 5 seconds(2,2,2)|(0,120,0)in next 4 seconds
