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...
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);
}
}
}