Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by kapa765 · Aug 03, 2020 at 07:48 PM · 2d gameenemypath

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;
     }
     
     
 }

 



}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
 
 
     }
 }

Comment
Add comment · Show 9 · 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
avatar image kapa765 · Aug 04, 2020 at 04:14 AM 0
Share

Thanks for quick anwer! I will try this code when im at home.

avatar image kapa765 · Aug 04, 2020 at 05:07 PM 0
Share

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"

avatar image kapa765 kapa765 · Aug 04, 2020 at 05:08 PM 0
Share

It showed error on line with "if (Vector3.Distance(transform.position, pathList[pathIndex].position) <= entryRadius)" with red line under pathList[pathIndex].position

avatar image unity_ek98vnTRplGj8Q kapa765 · Aug 04, 2020 at 05:10 PM 0
Share

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;
             }
 
 
         }
avatar image kapa765 unity_ek98vnTRplGj8Q · Aug 04, 2020 at 05:31 PM 0
Share

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"

Show more comments
avatar image
0

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

Comment
Add comment · Show 1 · 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
avatar image kapa765 · Aug 03, 2020 at 08:08 PM 0
Share

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

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

172 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

Related Questions

Multiple enemy prefabs moving towards a single goal. Need help! 1 Answer

Enemy's initial attack doesn't deal damage to player 1 Answer

Enemy offset, gallaga style game (space invaders) 0 Answers

How to move an object on a certain path 1 Answer

How could i access to a specific collider box if it is in a children of the main Object? The script is in the main object. 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