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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with AI [2D]

Discussion in '2D' started by FloatyPixel, Dec 19, 2015.

  1. FloatyPixel

    FloatyPixel

    Joined:
    Dec 11, 2015
    Posts:
    7
    Hello

    1st part -
    So, enemies should start coming from random points from outside the screen and pass somewhere close to the middle.
    That means - Few Starting points outside of the screen and few ending points somewhere in the middle of the screen. But they shouldn't stop when they reach end point, they should just continue and when they go outside of the screen again they should get destroyed.
    - Spawn enemies until Player killed 50 enemies

    2nd part -
    When enemies are traveling and if Player is in their line of sight they should start following the player, and when player exits their line of sight they should just continue going forward.

    UnityAI.png

    Have a nice day!
    - FloatyPixel
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    start easy :)
    make 2 empty gameobjects: topleft, topright
    make "spawn" c# script
    Code (CSharp):
    1.  
    2. //points for min/max spawn
    3. public GameObject topleft, topright;
    4. // spawn timer
    5. public float spawntime;
    6. //how much enemy
    7. public int maxenemy;
    8. //for timer, i cannot understand corutine well :)
    9. float maxtime;
    10. //your enemy prefab
    11. public GameObject enemy;
    12. void Start() {
    13. maxtime = spawntime;
    14. }
    15. void Update () {
    16. //minus time everyframe
    17. spawntime -= Time.deltaTime;
    18. if (spawntime < 0) {
    19. //reset for new cicle
    20. spawntime = maxtime;
    21. //random point between topleft - topright
    22. float rndX = Random.Range (topleft.transform.position.x, topright.transform.position.x);
    23. //make position for spawn on top-line
    24. Vector3 pos = new Vector3 (rndX, topleft.transform.position.y, 0);
    25. //spawn enemy
    26. Instantiate (enemy, pos, transform.rotation);
    27. // -1 enemy
    28. maxenemy-=1;
    29. if (maxenemy <= 0) {
    30. //you can destroy spawner or just disable it
    31. destroy (gameobject);
    32. }
    33. }
    34. }
    35.  
    make enemy prefab, i dont know, how you will move it. If enemy will run to the "somewhere" on green square (on my picture):

    float rndPosX = Random.Range (-1, 1);
    float rndPosY = Random.Range (-1, 1);
    Vector3 pos = new Vector3 (rndPosX, rndPosY, 0);
    //then find the direction between start end end (middle)
    Vector3 dir = pos - gameobject.transform.position;
    //if your enemy has rigibody2D, then
    rigidbody2d.velocity = new Vector3 (dir.x, dir.y,0);
    //maybe you should normalize vector dir for constant speed: dir = Vector 3.Normalize (dir)

    for line of sight try. Physic2D.OverlapCircle, if player is in it, then switch Vector3 pos with player.transform.position.
     

    Attached Files:

    Last edited: Dec 19, 2015
  3. FloatyPixel

    FloatyPixel

    Joined:
    Dec 11, 2015
    Posts:
    7
    Thank you! That helped me a lot
     
  4. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  5. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    I did something really similar to this for a 2d game! Let me explain how I did it, it might give you some ideas (even though I agree that you should start simple!) :)

    First, I use UnityEngine.Random.insideUnitCircle.normalized to randomly place an enemy on a circle. I then multiply this value with a value that is large enough to make sure that the enemies always spawn outside of the screen.

    Next I took a random position in the center of the screen (using a similar method as above) and I subtracted this from the position of the enemy (and normalized the result) to get the direction from the enemy to the center.

    Finally, I made the enemy move in that direction at a certain speed, until the enemy was outside of the screen. When the enemy was outside of the screen, I would reuse this enemy by simple calling the first 2 methods again (to position and give direction to the enemy).