title: How to rotate object while moving in unity?
body: <p>I need to rotate car along the path, but the code doesn't work correct. It moves as a firework. I guess that the problem is in moving from z to x axis. I dont have enough knowledge to solve this problem. Help!</p>
  
  <p><a href="https://i.stack.imgur.com/yVu9u.png" rel="nofollow"><img src="https://i.stack.imgur.com/yVu9u.png" alt="enter image description here"></a></p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class PlayerController : MonoBehaviour
  {
  
      public Transform StartPoint;
      public Transform[] EndPoints;
      public Transform[] AngleWaypoints;
      public float MoveSpeed;
  
  
      private Transform TargetPoint;
      //private Transform EndPoint;
      private float DistanceToPoint = 0f;
      private int RandomValue;
      private Transform[] Waypoints;
      private int wpIndex = 0;
      private Quaternion lookRotation;
      private Vector3 direction;
  
      // Use this for initialization
      void Start ()
      {
          RandomValue = Random.Range (1, EndPoints.Length);
  
          int count = 0;
          switch (RandomValue) {
          case 0:
              //Waypoints = StartPoint + EndPoints [RandomValue];
              count = 2;
              Waypoints = new Transform[count];
              Waypoints [0] = StartPoint; 
              Waypoints [1] = EndPoints[RandomValue];
              break;
          case 1:
              //Waypoints = StartPoint + AngleWaypoints + EndPoints[RandomValue];
              count = 2 + AngleWaypoints.Length;
              Waypoints = new Transform[count];
              Waypoints [0] = StartPoint;
              for(int i=0;i&lt;AngleWaypoints.Length;i++){
                  Waypoints [i + 1] = AngleWaypoints [i];
              }
              Waypoints [count-1] = EndPoints[RandomValue];
  
              break;
          }
  
          TargetPoint = Waypoints [wpIndex];
  
      }
  
      // Update is called once per frame
      void Update ()
      {
          DistanceToPoint = Vector3.Distance (transform.position, TargetPoint.position);
          if (DistanceToPoint &gt; 1) {
              direction = (TargetPoint.position - transform.position)/2 + transform.position;
  
              Vector3 dir = TargetPoint.position - transform.position;
              lookRotation = Quaternion.LookRotation(dir);
              transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime);
              Vector3 v = direction / DistanceToPoint * MoveSpeed * Time.deltaTime;
              transform.Translate (v);
  
          } else {
              wpIndex++;
              if (wpIndex &gt; Waypoints.Length - 1) {
                  wpIndex = 0;
                  Destroy(this.gameObject);
              }
              TargetPoint = Waypoints[wpIndex];
          }
      }
  }
  </code></pre>