2024-12-27 14:11:36 +02:00

49 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarDrivingRoutine : MonoBehaviour
{
public AudioSource _stopSound;
public AudioSource _tireSound;
public Waypoint _waypoint;
public float speed = 5f; // Movement speed
public float rotationSpeed = 5f; // Rotation speed
public float waypointProximityThreshold = 0.5f; // Distance to consider "close enough" to a waypoint
// Update is called once per frame
void Update()
{
if (_waypoint == null) return;
// Move towards the waypoint
Vector3 targetPosition = _waypoint.transform.position;
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
// Rotate towards the desired rotation
float targetRotation = _waypoint.DesiredRotation;
Quaternion desiredRotation = Quaternion.Euler(0, targetRotation, 0);
transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRotation, rotationSpeed * Time.deltaTime);
// Check if close enough to the waypoint
if (Vector3.Distance(transform.position, targetPosition) <= waypointProximityThreshold &&
Quaternion.Angle(transform.rotation, desiredRotation) <= 1f)
{
// Proceed to the next waypoint
_waypoint = _waypoint.Next;
if (_waypoint == null)
{
// Optional: Play stop sound when no more waypoints
_stopSound?.Play();
}
else
{
// Optional: Play tire sound when moving to the next waypoint
_tireSound?.Play();
}
}
}
}