Search Unity

Question Need help!

Discussion in 'Scripting' started by mahito2932, Feb 1, 2023.

  1. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    I'm trying to figure out how i can implement enemies that choose between 3 lines randomly and move toward to destination. Of course i don't want to use NavMeshAI because in my case i'm using transform.Translate and checking the distance between the enemy position and the end. But my question is how can i tell to the enemy follow separate lines but reach the same exact end position?
    Any help will be appreciated! :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Steps to success:

    - define your movement paths (lines) however you like (in scene, via code, via math, whatever)

    - put reference to those paths into some form of collection (List<T> or array perhaps?)

    - choose randomly from the collection

    - tell your enemy to use a particular path in their patrolling

    Excellent tutorials are available for 100% of the above steps.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  3. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115

    I think I didn't express myself well.
    I already google it for months and saw a lot of youtube tutorials and even check multiple time Unity Learn Waypoints but no one of them cover this part because i don't need to make enemy patroling or something else.
    I think my questions wasn't understand at all.

    What all tutorial does is just showing how randomly enemy should go around the lines or path and reach end destination but this is not what i'm looking for at all. My questions is how to tell enemy to go in 3 separate lines RANDOMLY but reach end position and tell that enemy shouldn't switch their position until they don't reach the point because enemy after you declare switch random position they actually switching them everytime to reach in a better way end destination but that's completely opposite logic of what i'm looking for.
    As i mentioned i already have a working code but it's just follow only 1 line and i have no idea on how to change that to switch in multiple lines.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Then you are not understanding what you are reading.

    My simple WaypointGizmo script already does ALL of this here:

    https://gist.github.com/kurtdekker/c2246d3780b93fe4e023695eb947e85d

    You can make as many different paths as you like and pick randomly among them.

    You can make 100% of the paths start at the same point and end at the same point. It doesn't care.

    Here is a scene screenshot of me using the above code directly in my ATGM game to control the tank travel.

    A tank will randomly pick ONE path and travel the full path.

    image.png
    here's what it looks like in editor:

    Screen Shot 2023-02-01 at 8.06.40 AM.png

    Here is the game, see for yourself:

    Appstore: https://itunes.apple.com/us/app/atgm/id1135587120
    GooglePlay: https://play.google.com/store/apps/details?id=com.plbm.atgm
    Android TV: https://play.google.com/store/apps/details?id=com.plbm.atgmtv
     
  5. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    And that's exactly opposite logic that i'm looking for.
    In your actuall example you show that tank will only pick ONE path to travel the FULL path and that exactly what all tutorials does and show like this example.
    The reason why i'm asking that in this forum because there's no documentation at all. How can i learn something that this something doesn't exists. I'm asking here about this because maybe someone know how to make that. In your case you just showing that once tank decide to go in this path it will not change it until it reach the end position and that's the exact point what i'm mention right now.
     
  6. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    that's the problem what if you have only one start point but you should tell enemy to go for example to the left or right or even forward without remake the whole path to reach end point
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    you say this:

    But you also say this:

    I think it might be most useful for you to link to a video of a game that has this mechanic.

    If you cannot, then perhaps sketch it out and post a picture.
     
  8. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    Actually i'm not creating any game i just try to understand mechanics and learn. But there's one point that i actually want to learn but i'm stack a lot on this. As you said previously FIX this error and move forward that's what i want to do but i really need a help... and if someone can explain me the process.

    I did some quickly paint.

     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You may also need to review how you attach images. Just use the forum mechanism, not an external link.

    Screen Shot 2023-02-01 at 9.15.47 AM.png
     
  10. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    I just misplaced the link that wasn't an image that was a document. Sorry
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Just chain two waypoint collections together:

    - have one path to go from start to the decision point
    - at the decision point you're DONE with that path
    - at the decision point now select from three remaining path ways randomly for each enemy
     
  12. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    What do you mean by chain two waypoint? can you show me some example, please?
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Give your enemy a path to go from start to decision point.

    When he gets there, give him another path randomly to get to the end.

    If any of this is mysterious, you just need to start getting some experience with patrol / pathing and AI in general. This is all super-super-super basic stuff and there is no chance I can type it all in this box.
     
  14. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    Just as a single path is a list of points, you can have a list of paths.

    Just as you navigate a path by moving from point to point in the list, you can navigate a series of paths by moving from path to path.
     
  15. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    Okay than can you link some of them that cover this part since that super easy task. And i believe that this won't use NavMeshAI because i just don't want to use that at all.
     
  16. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    Is there any example that i could read or see?
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You can use google just like I can.

    If you cannot, then start by learning how to search google.

    You can use navmesh, you can not, you can use Leantween.

    All of these things are possible.

    Doing these things will bring you closer to your success.

    Posting more likely will not bring you closer to your success.

    Be sure you do the tutorials you need correctly as you build your knowledge.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:
     
  18. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    But there isn't any example on google search no one covered this logic at all. What i'm supposed to search for if there's nothing to search..
     
  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    As I said above,

    Do THREE different tutorials fully, from start to finish.
     
  20. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    I already did Unity course that covering Waypoints , path finding and AI but what they cover is just the same things that you show's me as example including NavMesh or not.