Search Unity

Other TRAFFIC CAR AI CONTROLLER

Discussion in 'Scripting' started by umut81, Nov 2, 2022.

  1. umut81

    umut81

    Joined:
    Nov 2, 2022
    Posts:
    1
    Hello. I have a waypoint script file. With this file, I can create lines and connecting points that are not visible during the game in the form of drawline and drawwire sphere in the project. I need a script that can be loaded on a vehicle and follow this path, which has reactions such as wheel collider features and can only follow without sticking to the road line. Can you help me. I'm attaching the code I used for the waypoint below. help please
    -----------------------------------------------------------------------------------------------------------------------------

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Waypoints : MonoBehaviour
    {
    [Range(0f, 2f)]
    [SerializeField] private float WaypointSize = 1f;
    private void OnDrawGizmos()
    {
    foreach (Transform t in transform)
    {
    Gizmos.color = Color.white;
    Gizmos.DrawWireSphere(t.position, WaypointSize);
    }
    Gizmos.color = Color.white;
    for (int i = 0; i < transform.childCount - 1; i++)
    {
    Gizmos.DrawLine(transform.GetChild(i).position, transform.GetChild(i + 1).position);
    }
    Gizmos.DrawLine(transform.GetChild(transform.childCount - 1).position, transform.GetChild(0).position);
    }
    public Transform GetNextWaypoint(Transform currentWaypoint)
    {
    if (currentWaypoint == null)
    {
    return transform.GetChild(0);
    }
    if (currentWaypoint.GetSiblingIndex() < transform.childCount - 1)
    {
    return transform.GetChild(currentWaypoint.GetSiblingIndex() + 1);
    }
    else
    {
    return transform.GetChild(0);
    }
    }
    }