• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TRAsmile · 3 days ago · pathfindingcar game

Can't add new gameobject (car) on a specific node of a path (car AI)

So there is a list of nodes which are children of a path. I built tens of those and made cars randomly spawn on each of the nodes with a small delay with an intention to make them continuing their move to next node forgetting about nodes before them. But every spawned car is just moving to the first node.

I tried to implement some ideas to every script of the game, but due to the fact I am a noob I ve failed.

Here is a script for the "path"

 public class path : MonoBehaviour
 {
      public Color lineColor;
     
         private List<Transform> nodes = new List<Transform>();
     
          void OnDrawGizmosSelected()
         {
             Gizmos.color = lineColor;
     
             Transform[] pathTransforms = GetComponentsInChildren<Transform>();
             nodes = new List<Transform>();
     
             for(int i = 0; i < pathTransforms.Length; i++)
             {
                 if(pathTransforms[i] != transform)
                 {
                     nodes.Add(pathTransforms[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.DrawLine(previousNode, currentNode);
                 Gizmos.DrawSphere(currentNode, 0.3f);
             } 
     }

And here is a piece of the AI from "carEngine" script

 public class CarEngine : MonoBehaviour
 {...
 public Transform path;
 private List<Transform> nodes;
 private int currentNode = 0;
 ...
 void Start()
     {
 
         GetComponent<Rigidbody>().centerOfMass = centerOfMass;
 
         Transform[] pathTransforms = path.GetComponentsInChildren<Transform>();
         nodes = new List<Transform>();
 
         for (int i = 0; i < pathTransforms.Length; i++)
         {
 if (pathTransforms[i] != path.transform)
             {
                 nodes.Add(pathTransforms[i]);
             }
         }
 }
  private void FixedUpdate()
     {
 ...
 CheckWaypointDistance();
 }
 
 private void CheckWaypointDistance()
     {
 
         if (Vector3.Distance(transform.position, nodes[currentNode].position) < 5f)
         {
                 if (currentNode == nodes.Count - 1)
             {
                 currentNode = 0;
             }
             else
             {
                 currentNode++;
             }

How can I make new spawned cars ignore first nodes and make them follow the nearest next one?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

make a self-driving car, combine Navimesh and waypoint? 1 Answer

A* 2D Pathfinding not working with prefabs? 0 Answers

Using NavMesh creates serious lag spikes 0 Answers

C# Min/Max Rotation based on Float? 1 Answer

How to make player move from one track to the other gradually? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges