• 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 nsane808 · Jan 04, 2017 at 07:28 PM · gameobjectspawningprefab-instancewaypoint system

Prefab to automatically follow a waypoint path?

I have a car game object with scripts that move it along a path. It works great exactly how I want it to work.

I then turn it into a prefab to spawn at a certain point on a road. I made a game controller to instantiate the prefab. It works and the car spawns where I want it to......but doesn't move along the path. It just sits there.

When I first wrote the scripts for the game object I made it so that I had to manually drag the waypoint path object to a public variable for it to follow. And I assumed that when I made the car into a prefab it kept that variable when it spawns. It doesn't.

So my question is how do I get my spawned car to automatically find the path to follow and actually follow?

Here is my initial code for moving the car on the path:

 using UnityEngine;
 using System.Collections;
 
 public class moveOnPath : MonoBehaviour
 {
 
     public waypointPath PathToFollow;
 
     public int CurrentWayPointID;
     public float speed;
     public float reachDistance;
     public float rotationSpeed;
     public string pathName;
 
     Vector3 last_position;
     Vector3 current_position;
 
 
  
 
     void Start()
     {
         PathToFollow = GameObject.Find(pathName).GetComponent<waypointPath>();
         last_position = transform.position;
     }
 
 
     void Update()
     {
         float distance = Vector3.Distance(PathToFollow.nodes[CurrentWayPointID].position, transform.position);
         transform.position = Vector3.MoveTowards(transform.position, PathToFollow.nodes[CurrentWayPointID].position, Time.deltaTime * speed);
 
         var rotation = Quaternion.LookRotation(PathToFollow.nodes[CurrentWayPointID].position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
 
         if (distance <= reachDistance)
         {
             CurrentWayPointID++;
         }
 
         if (CurrentWayPointID >= PathToFollow.nodes.Count)
         {
             CurrentWayPointID = 0;
         }
     }
 }

And here is the code for the path:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Path : MonoBehaviour {
 
     public Color lineColor;
 
     public 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.DrawWireSphere(currentNode, 0.3f);
         }
 
 
     }
 
 
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kuivalappa · Mar 17, 2017 at 11:26 AM

try this one if it is 2d

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EnemyWaypoints : MonoBehaviour {

 public List<Transform> waypoints;
 private Transform currentWaypoint;

 public float speed = 5f;

 private float closeEnouth = 0.5f;
 int point = 0;
 


 void Start()
 {

     currentWaypoint = waypoints[point];
 }


 // Update is called once per frame
 void Update()
 {

     Quaternion rotation = Quaternion.LookRotation(waypoints[point].position - transform.position, transform.TransformDirection(Vector3.forward));
     transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);

     float dist = Vector3.Distance(waypoints[point].position, transform.position);
     transform.position = Vector3.MoveTowards(transform.position, waypoints[point].position, Time.deltaTime * speed);

     if (Vector3.Distance(this.transform.position, waypoints[point].position) < closeEnouth)
     {
         if (point + 1 < waypoints.Count)
             point++;
        

     }
 }

}

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 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

Related Questions

How can I have prefab spawned at the game's start and at a position? 0 Answers

How to get scripts to detect foreign variable change? 0 Answers

Why does GameObject.FindWithTag() get a wrong instance? 2 Answers

Instantiated bullet wont destroy 1 Answer

Instantiate more than 1 prefab 0 Answers


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