Search code examples
unity-game-enginepath2dmoveitween

Follow a path with Rotation


I am trying to move a gameobject with a defined path. I have used iTweenpath and successfully implemented it, but the problem is when I choose a curve path my gameobject do not rotate according to path. My project is 2d so only Z axis will work for rotation.

Below is the code :-

                iTween.MoveTo(gameObject , iTween.Hash("path" ,
                 iTweenPath.GetPath("CurvePath") , "time" , 10 
                 ,"orienttopath", true , "lookahead", 1.0f ,"axis", "z"));

Solution

  • I calculated the minimal distance between the previous point and current point in Coroutine and give that delta to transform.up or transform.forward. It works!

          IEnumerator RotateObject()
          {
           while (true) 
            {
            yield return new WaitForSeconds(0.001f);
                transform.up = transform.position - prev;
                prev = transform.position;
            }
          }