Search Unity

spawn enemies in a specific location in a 2d platformer

Discussion in '2D' started by theawesome1, Oct 5, 2020.

  1. theawesome1

    theawesome1

    Joined:
    Jul 21, 2020
    Posts:
    6
    I want enemies to spawn in a specific when the camera (or player) is close to the enemy, so the enemy won't fall off or go too far away. How would I go about doing that?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    There plenty of examples online. Just give them a good ol Google. First figure out how to spawn enemies or objects. Then you need to figure out where to spawn them. My suggestion is if you want them to always somewhere near, make the spawn position gameobject a child of the player. Maybe have a few of them so it can randomly select one of the options.

     
    TonyTheArtist likes this.
  3. theawesome1

    theawesome1

    Joined:
    Jul 21, 2020
    Posts:
    6
    @Cornysam Thank you. I will go try to try your suggestions.
     
    Cornysam likes this.
  4. theawesome1

    theawesome1

    Joined:
    Jul 21, 2020
    Posts:
    6
    If anyone is wondering, I made so if the camera's transform is a certain distance, the enemy will start moving or doing anything that it has to do.
    This is my code
    Code (CSharp):
    1.  public Transform lansform;
    2.     public SpriteRenderer sr;
    3.     public bool isright = true;
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         sr = GetComponent<SpriteRenderer>();
    8.      
    9.     }
    10.     private void OnCollisionEnter2D(Collision2D collision)
    11.     {
    12.         if (collision.gameObject.tag != "ground")
    13.         {
    14.             if (isright == true)
    15.             {
    16.                 isright = false;
    17.             }
    18.             else if (isright == false)
    19.             {
    20.                 isright = true;
    21.             }
    22.         }
    23.     }
    24.     private void OnTriggerEnter2D(Collider2D collision)
    25.     {
    26.      
    27.      
    28.     }
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.        
    33.         if (lansform.position.x + 10 >  this.transform.position.x)
    34.         {
    35.            
    36.             if (isright == true)
    37.             {
    38.                 transform.Translate(Vector2.right * Time.deltaTime);
    39.                 sr.flipX = false;
    40.             }
    41.             else if (isright == false)
    42.             {
    43.                 transform.Translate(Vector2.left * Time.deltaTime);
    44.                 sr.flipX = true;
    45.             }
    46.         }
    47.  
    48.  
    49.     }
    50.  
     
    SupKittyMeow likes this.