- Home /
AI car doesn't work?
I have made this code so that the car is going to follow the path, but the car doesn't follow the Path and continues just straight.`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Path : MonoBehaviour { public Color lineColor; public Color color = Color.white;
private List<Transform> Nodes = new List<Transform>();
void OnDrawGizmosSelected()
{
Gizmos.color = lineColor;
Transform[] pathTransform = GetComponentsInChildren<Transform>();
Nodes = new List<Transform>();
for (int i = 0; i < pathTransform.Length; i++)
{
if (pathTransform[i] != transform)
{
Nodes.Add(pathTransform[i]);
}
}
for (int i = 0; i < Nodes.Count; i++)
{
Vector3 currentNode = Nodes[i].position;
Vector3 previousNode = Vector3.zero;
if (i > 0)
{
previousNode = Nodes[i - 1].position;
}
else if (i == 0 && Nodes.Count > 1)
{
previousNode = Nodes[Nodes.Count - 1].position;
}
Gizmos.color = Color.white;
Gizmos.DrawLine(previousNode, currentNode);
Gizmos.DrawWireSphere(currentNode, 0.2f);
}
}
}
` that is the script for the Path
and this is the script for the AI car
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SteeringByItself : MonoBehaviour {
public Transform path;
public float maxSteerAngle = 180f;
public float speedForce = 2f;
private List<Transform> Nodes;
private int currentNode = 0;
// Use this for initialization
void Start () {
Transform[] pathTransform = path.GetComponentsInChildren<Transform>();
Nodes = new List<Transform>();
for (int i = 0; i < pathTransform.Length; i++)
{
if (pathTransform[i] != path.transform)
{
Nodes.Add(pathTransform[i]);
}
}
}
private void FixedUpdate()
{
ApplySteer();
Drive();
CheckWayPointDistance();
}
private void ApplySteer ()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
Vector3 relativeVector = transform.TransformPoint(Nodes[currentNode].position);
float newSteer = (relativeVector.x / relativeVector.magnitude) * maxSteerAngle;
rb.angularVelocity = newSteer;
}
private void Drive()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.AddForce(transform.up * speedForce);
}
private void CheckWayPointDistance()
{
if(Vector3.Distance(transform.position, Nodes[currentNode].position) < 0.5f)
{
if(currentNode == Nodes.Count -1)
{
currentNode = 0;
}
else
{
currentNode++;
}
}
}
}
What I'm doing wrong?
Answer by Maunda · Jul 09, 2017 at 01:03 PM
I Think You Forgot To Use Navigation
Bake Your Scene With Navmesh Its in your windows>Navigation
And There's Nothing Wrong With Your Script, The Only Thing You Have To Do Is Bake Your Scene In Navigation Cause AI Need Navmesh To Walk On To $$anonymous$$AYBE ? :/
I did what you sad but it doesn't work. Is this script also good for a 2D race game? and thank you for responding :)
Your answer
Follow this Question
Related Questions
How to Set the Game Speed Based on the Player's Current Score? 1 Answer
Button Enabling/Disabling using Collision Triggers? 1 Answer
Referencing variables from another script 2 Answers
C# UI list item drag onto 2d sprite in world space? 1 Answer
Object reference not set to the instance of an object 1 Answer