0
\$\begingroup\$

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

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Well, 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) \$\endgroup\$ Commented Jul 13, 2020 at 8:27

1 Answer 1

0
\$\begingroup\$

Answer was found. I forgot to calculate time steps for rotation as well. Now code looks like:

 private IEnumerator DoMove()
    {
        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;
                float rtime = Quaternion.Angle(crot, wp.position.rotation) / (duration - counter) * Time.deltaTime;
                cube.transform.position = Vector3.MoveTowards(curpos, wp.position.position, time);
                cube.transform.rotation = Quaternion.RotateTowards(crot, wp.position.rotation, rtime);
                yield return null;
            }
            Debug.Log("Reached position: " + cube.transform.position + "|" + cube.transform.rotation.eulerAngles +" in time: " + Time.time);
            Debug.Break();
        }

    }

One row did the stuff:

float rtime = Quaternion.Angle(crot, wp.position.rotation) / (duration - counter) * Time.deltaTime;

Thank you for the comments

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.