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); } } } 1Related questions 2 Why does my Teleport Move not function the way I want it to? 0 Moving perpendicular to an object 0 Move and Rotate Camera towards Target Game Object Related questions 2 Why does my Teleport Move not function the way I want it to? 0 Moving perpendicular to an object 0 Move and Rotate Camera towards Target Game Object 0 Object with many children does not show up in the middle although it has coordinates set to 0, 0, 0 Load 1 more related questions Show fewer related questions
Reset to default