Problem with 'Look Rotation Viewing Vector Is Zero'

I'm trying to make a game where a car comes out of a garage and then has to drive to a place but when the car leaves it's in the ground for a second

That the car starts normally and does not sit in the ground for a while My code works with points where the car has to go.

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarMovement : MonoBehaviour
{
    private bool uitGarageRijden = false;
    private List<Transform> PathPoints = new List<Transform>();
    [SerializeField] private float moveSpeed = 0.1f;
    [SerializeField] private float rotationSpeed = 5f; // Adjust the rotation speed as needed
    private int pointsIndex;

    private void Update()
    {
        if (uitGarageRijden)
        {
            if (pointsIndex <= PathPoints.Count - 1)
            {
                Transform targetPoint = PathPoints[pointsIndex];
                Vector3 moveDirection = (targetPoint.position - transform.position).normalized;

                // Move the car towards the target point
                transform.position = Vector3.MoveTowards(transform.position, targetPoint.position,                            moveSpeed * Time.deltaTime);

                if (moveDirection != Vector3.zero)
                {
                    // Calculate the rotation target
                    Quaternion targetRotation = Quaternion.LookRotation(moveDirection, Vector3.up);

                    // Gradually rotate the car towards the target rotation
                    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
                }

                if (transform.position == targetPoint.position)
                {
                    pointsIndex++;
                }
            }
        }
    }

    public void UitGarageRijden()
    {
        uitGarageRijden = true;
    }

    public void UitGarage()
    {

    }

    public void AddPointsToList(List<Transform> points)
    {
        foreach (Transform point in points)
        {
            PathPoints.Add(point);
        }
    }
}
1
James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article