- Home /
 
Hi, im trying to make some simple 2d enemy path but somehow its not working as it should, btw im using Unity and learnig C# for 5 weeks so im sorry if its like a very simple question.
so if i play game enemy is going where it should go but not doing next thing i mean going to "path2"
using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.IO; using UnityEngine;
public class EnemyPath : MonoBehaviour { public Transform path1; public Transform path2;
 bool onPath1;
 bool toPath1;
 bool toPath2;
 bool onPath2;
  
 
 private void Start()
 {
     toPath1 = true;
     
 }
 void Update()
 {
     if (onPath1 == true)
     {
         this.transform.position = path1.transform.position;
         onPath2 = false;
         toPath1 = false;
         toPath2 = true;
         onPath1 = false;
     }
     
      if (onPath2 == true)
     {
         this.transform.position = path2.transform.position;
         onPath2 = false;
         toPath1 = true;
         toPath2 = false;
         onPath1 = false;
     }
     
     if (toPath1 == true)
     {
         transform.position = Vector2.MoveTowards(transform.position, path1.transform.position, 3.5f * Time.deltaTime);
         onPath2 = false;
         toPath1 = true;
         toPath2 = false;
         onPath1 = false;
     }
     
     if (toPath2 == true)
     {
         transform.position = Vector2.MoveTowards(transform.position, path2.transform.position, 3.5f * Time.deltaTime);
         onPath2 = false;
         toPath1 = false;
         toPath2 = true;
         onPath1 = false;
     }
     
     
 }
 
 
               }
Answer by unity_ek98vnTRplGj8Q · Aug 03, 2020 at 10:47 PM
Let me suggest some reorganized code, I don't think you are going to get what you want by going down this path.
Here I use a list of transforms to represent the path, and an int to keep track of which path waypoint we are moving towards. This helps reduce a lot of redundant code.
 //Make sure you populate the pathList variable in the inspector before you run this script
 public class EnemyPath : Monobehavior {
     public List<Transform>() pathList; //Here you can give it as many path waypoints as you want
     public float speed = 3.5f;
     public float entryRadius = 0.5f;
     public bool loopPath = false;
     //Keeps track of which waypoint we are going to
     private int pathIndex = 0; 
 
     void Update(){
 
         //Only move if we have a valid path index
         if(pathIndex < pathList.Count){
             transform.position = Vector2.MoveTowards(transform.position, pathList[pathIndex], speed * Time.deltaTime);
 
             //If we are close enough to our current target, move to the next target in the list
             if(Vector3.Distance(transform.position, pathList[pathIndex].position) <= entryRadius){
                 pathIndex ++;
             }
         } 
         else if(loopPath){
             pathIndex = 0;
         }
 
 
     }
 }
 
              Thanks for quick anwer! I will try this code when im at home.
so i tried to implement this script to my object but got like 4-5 errors and tried to reduce errors, got 1 left, please have a look at this script!, and tell me what could be wrong, i dont see "paths" in inspector, i guess because of error.
using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.IO; using UnityEditor; using UnityEngine;
//$$anonymous$$ake sure you populate the pathList variable in the inspector before you run this script public class EnemyPath : $$anonymous$$onoBehaviour { public List pathList; //Here you can give it as many path waypoints as you want public float speed = 3.5f; public float entryRadius = 0.5f; public bool loopPath = false; //Keeps track of which waypoint we are going to private int pathIndex = 0; Vector3 pathList1;
 void Update()
 {
     //Only move if we have a valid path index
     if (pathIndex < pathList.Count)
     {
         transform.position = Vector2.$$anonymous$$oveTowards(transform.position, pathList[pathIndex], speed * Time.deltaTime);
         //If we are close enough to our current target, move to the next target in the list
         if (Vector3.Distance(transform.position, pathList[pathIndex].position) <= entryRadius)
         {
             pathIndex++;
         }
     }
     else if (loopPath)
     {
         pathIndex = 0;
     }
 }
   
 
                  }
Error: CS1503 Argument 2: cannot convert from "UnityEngine.Transform" to "UnityEngine.Vector2"
It showed error on line with "if (Vector3.Distance(transform.position, pathList[pathIndex].position) <= entryRadius)" with red line under pathList[pathIndex].position
Yep my bad I had a couple typos in here, here is the script without errors
         public List<Transform> pathList; //Here you can give it as many path waypoints as you want
         public float speed = 3.5f;
         public float entryRadius = 0.5f;
         public bool loopPath = false;
         //Keeps track of which waypoint we are going to
         private int pathIndex = 0;
 
         void Update()
         {
 
             //Only move if we have a valid path index
             if (pathIndex < pathList.Count)
             {
                 transform.position = Vector2.$$anonymous$$oveTowards(transform.position, pathList[pathIndex].position, speed * Time.deltaTime);
 
                 //If we are close enough to our current target, move to the next target in the list
                 if (Vector3.Distance(transform.position, pathList[pathIndex].position) <= entryRadius)
                 {
                     pathIndex++;
                 }
             }
             else if (loopPath)
             {
                 pathIndex = 0;
             }
 
 
         }
 
                  Script works without errors now but, another rookie question, how to assign object transform positions to a transform list? it's my first time i see transform "list"
Answer by kapa765 · Aug 03, 2020 at 07:55 PM
I want to mention that game is top down without gravity if its changes anything
also, i did a debug log on toPath1 and if enemy reaches destination its like not changing to other bool but staying on the same, whole time toPath1 debug spam on console.
Your answer