Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question adding a stopping mechanic to Antarsoft's enemy patrol system

Discussion in 'Scripting' started by taniesha09, Nov 8, 2022.

  1. taniesha09

    taniesha09

    Joined:
    Oct 23, 2022
    Posts:
    2
    hi there, i was wondering if there was any way to add a mechanic to make the enemy stop for a few seconds after it reaches its goal point. Im new so can only really follow tutorials but dont exactly know how to edit them to fit my needs. Thank you for helping this newbie lol :)

    public class Enemy : MonoBehaviour
    {

    //ref to waypoin
    public List<Transform> points;
    //Int value for next point index
    public int nextID = 0;
    //value that applies to ID for changing
    int idChangeValue = 1;
    //speed of movement
    public float speed = 2;



    private void Reset()
    {
    Init();
    }
    void Init()
    {
    //box collider trigger
    GetComponent<BoxCollider2D>().isTrigger = true;

    //Create root object
    GameObject root = new GameObject(name+"_Root");

    //reset position of root to enemey object
    root.transform.position = transform.position;
    //set enemy object as child of rooth
    transform.SetParent(root.transform);
    //create waypoint objects
    GameObject waypoints = new GameObject("waypoints");
    //reset waypoints position to root

    //make waypoint object child of root
    waypoints.transform.SetParent(root.transform);
    waypoints.transform.position = root.transform.position;
    //create two poinst and reset their position to waypoint objects

    //make the points children of waypoint object
    GameObject p1 = new GameObject("Point1"); p1.transform.SetParent(waypoints.transform); p1.transform.position=root.transform.position;
    GameObject p2 = new GameObject("Point2"); p2.transform.SetParent(waypoints.transform); p2.transform.position=root.transform.position;

    //Init points list then add the points to it
    points = new List<Transform>();
    points.Add(p1.transform);
    points.Add(p2.transform);


    }


    private void Update()
    {
    MoveToNextPoint();
    }

    void MoveToNextPoint()
    {
    //get the next point tranform
    Transform goalPoint = points[nextID];
    //Flip the enemy to face points direction
    if(goalPoint.transform.position.x>transform.position.x)
    transform.localScale = new Vector3(1, 1 , 1);
    else
    transform.localScale = new Vector3(-1, 1 , 1);
    //Move enemy towards goal point
    transform.position = Vector2.MoveTowards(transform.position, goalPoint.position, speed*Time.deltaTime);
    //check distance between enemy and goal point to trigger next point
    if(Vector2.Distance(transform.position, goalPoint.position)<0.2f)
    {
    // check if we are at the end of the line (change -1)
    if(nextID == points.Count -1 )
    idChangeValue = -1;

    // check if we aree at the start of the line (change +1)
    if(nextID == 0)
    idChangeValue = 1;
    // apply change on nextID
    nextID += idChangeValue;

    }

    }

    }
     
    Last edited: Nov 9, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    This is usually handled by some kind of simple state machine.

    The simplest would be a single float variable that indicates "I Am waiting"

    Then, while that variable is nonzero:
    - count down that variable by Time.deltaTime
    - inhibit enemy movement

    Whenever it arrives at a destination, set that variable to the desired loiter time.

    This is the general case:

    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297

    GunHeat (gunheat) spawning shooting rate of fire:

    https://forum.unity.com/threads/spawning-after-amount-of-time-without-spamming.1039618/#post-6729841

    ALSO, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    That will come when you complete Step #2 of my "Following Tutorials Properly" guide:

    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!
     
  3. taniesha09

    taniesha09

    Joined:
    Oct 23, 2022
    Posts:
    2
    After some trial and error i got it to work! i had a look back at why we were doing some of the coding and it helped me to figure out where to place the stop code. Thank you so much!! m
     
    Kurt-Dekker likes this.