113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Простой автомобиль городского трафика: кинематически едет по замкнутому маршруту по правой полосе,
|
|
/// плавно поворачивает на перекрестках и останавливается перед препятствием (игрок, другая машина).
|
|
/// </summary>
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
public class TrafficCar : MonoBehaviour
|
|
{
|
|
public Vector3[] waypoints;
|
|
public float cruiseSpeedKmH = 40f;
|
|
public int startIndex;
|
|
|
|
private Rigidbody rb;
|
|
private int target;
|
|
private float speed;
|
|
private Transform[] wheels;
|
|
private Collider[] ownColliders;
|
|
private float halfLength = 2.3f;
|
|
|
|
private void Awake()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
rb.isKinematic = true;
|
|
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
|
ownColliders = GetComponentsInChildren<Collider>();
|
|
|
|
Bounds b = EngineBayManager.LocalRendererBounds(transform, transform, null);
|
|
halfLength = Mathf.Max(1.5f, b.extents.z);
|
|
|
|
var list = new System.Collections.Generic.List<Transform>();
|
|
foreach (Transform t in GetComponentsInChildren<Transform>())
|
|
if (t.name.ToLowerInvariant().StartsWith("wheel_") && t.GetComponent<Renderer>() != null) list.Add(t);
|
|
wheels = list.ToArray();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (waypoints == null || waypoints.Length < 2)
|
|
{
|
|
enabled = false;
|
|
return;
|
|
}
|
|
target = (startIndex + 1) % waypoints.Length;
|
|
speed = cruiseSpeedKmH / 3.6f * 0.5f;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (waypoints == null || waypoints.Length < 2) return;
|
|
|
|
Vector3 pos = rb.position;
|
|
Vector3 goal = waypoints[target];
|
|
goal.y = pos.y;
|
|
Vector3 to = goal - pos;
|
|
float dist = to.magnitude;
|
|
|
|
if (dist < 3f)
|
|
{
|
|
target = (target + 1) % waypoints.Length;
|
|
return;
|
|
}
|
|
|
|
// Сбавляем перед поворотом
|
|
float desired = cruiseSpeedKmH / 3.6f;
|
|
if (dist < 18f) desired *= 0.45f;
|
|
|
|
// Препятствие впереди — тормозим
|
|
if (ObstacleAhead(out float obstacleDist))
|
|
desired = obstacleDist < 6f ? 0f : desired * 0.3f;
|
|
|
|
speed = Mathf.MoveTowards(speed, desired, (desired < speed ? 9f : 3.5f) * Time.fixedDeltaTime);
|
|
|
|
Quaternion look = Quaternion.LookRotation(to.normalized, Vector3.up);
|
|
Quaternion rot = Quaternion.RotateTowards(rb.rotation, look, Mathf.Lerp(25f, 70f, speed / 12f) * Time.fixedDeltaTime);
|
|
Vector3 forward = rot * Vector3.forward;
|
|
|
|
rb.MoveRotation(rot);
|
|
rb.MovePosition(pos + forward * speed * Time.fixedDeltaTime);
|
|
|
|
if (wheels != null)
|
|
{
|
|
float spin = speed * Time.fixedDeltaTime / 0.33f * Mathf.Rad2Deg;
|
|
foreach (Transform w in wheels) w.Rotate(spin, 0f, 0f, Space.Self);
|
|
}
|
|
}
|
|
|
|
private bool ObstacleAhead(out float distance)
|
|
{
|
|
distance = float.MaxValue;
|
|
Vector3 origin = rb.position + Vector3.up * 0.8f + transform.forward * (halfLength + 0.3f);
|
|
RaycastHit[] hits = Physics.BoxCastAll(origin, new Vector3(0.9f, 0.5f, 0.2f), transform.forward, transform.rotation, 14f, ~0, QueryTriggerInteraction.Ignore);
|
|
bool found = false;
|
|
foreach (RaycastHit h in hits)
|
|
{
|
|
if (IsOwn(h.collider)) continue;
|
|
if (h.collider.attachedRigidbody == null && h.collider.GetComponentInParent<CharacterController>() == null) continue; // статика (бордюр, дом) — не препятствие на дороге
|
|
if (h.distance < distance)
|
|
{
|
|
distance = h.distance;
|
|
found = true;
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
private bool IsOwn(Collider c)
|
|
{
|
|
foreach (Collider o in ownColliders) if (o == c) return true;
|
|
return false;
|
|
}
|
|
}
|