Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I've got two problems that need solutions...

Discussion in 'Scripting' started by San_Holo, Nov 24, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi,

    Can you help me thrash out a solution to two problems or ideas I need to complete in my game, one of them is easy really... my game-scene will repeat but my background textures to the quads used as a back ground will change every-time so...I could create an array and dump my textures into it via the inspector and call them according to what level I'm on or progressively from 1 ++ onwards... got it, but I may need help with the next one...

    Imagine a 2D scrolling vertical shump, the 'play-area' scrolls with the camera in an upwards direction just to move the play-area-trigger towards my hand-placed enemy spawner objects which just spit out enemies when triggered and so you move up through the level triggering these spawner's till they have all passed etc etc etc, but how could I randomly place these spawner objects rather than having to place them by hand in my game-scene, I would/should create a system whereby these spawner's are randomly placed within a set boundary and a set number increasing with every level++ and bingo, so if you could enlighten me or teach me or give me an idea on how to do it then I'll try to code it up myself, but I'm just learning but also need to see how these ideas and flourish and grow, I'm having a great time with Unity so far getting into these creative problems and finding good solutions, but I could use some help from time to time, thanks very much.

    oh btw the game in question is HERE if your wondering, cheers
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    Nicely done. I found the game very fun to watch and it looks fun to play!

    Are you simply trying to place a spawner somewhere above the screen such that enemies appear from the top of the screen despite the camera location? Because if so, you could always use Camera.ViewportToWorldPoint to place a spawner above the Camera's view. You can even randomize the x-location so it looks like enemies are spawned from different spots of the screen each time.

    You would have to make a function for how many of these spawners should occur, and at what rate, depending on the level. For example, level 1 could map to 1 spawner at a rate of 50 world units traveled (the 2nd number is really just a guess for the sake of the example), level 2 could be 2 spawners at a rate of 50 world units traveled, level 3 could be 2 spawners at a rate of 40 world units traveled... etc.

    Again, these are really just rough numbers. Don't feel limited by these numbers.. in fact you may want to have a range of spawners that occur at a time based on the level (for example, level 1 is just 1, level 2 could be 2-3, level 3 could be 3-3, etc)
     
    San_Holo likes this.
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Thanks Stoven, that Camera.ViewportToWorldPoint looks promising, but yes your right, enemies appear from the top as the camera is locked to the player-area and which moves through it's Y axis slowly hitting these spawn-triggers as it moves through space & time... bit like Dr Who :) no.. so yeah exactly the idea I had was to increase the amount of spawn objects per level and with certain parameters changed per level to, I'll do some more research on that Camera.ViewportToWorldPoint, thanks again.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    You could replace your spawn point transforms with an array in a script. You could have different arrays of points of spawn patterns. Or maybe a 2D array of points. Then when you spawn your enemies, you use the positions there relative to the player.

    Code (CSharp):
    1. Vector2[][] test;//spawn points
    2.  
    3. test = new Vector2[][]
    4.         {
    5.                         /*horizontal spawns*/
    6.             new Vector2[]
    7.             {
    8.                 new Vector2(0,0),
    9.                 new Vector2(5,0),
    10.                 new Vector2(10,0)
    11.             },
    12.                         /*vertical spawns*/
    13.             new Vector2[]
    14.             {
    15.                 new Vector2(0,0),
    16.                 new Vector2(5,0),
    17.                 new Vector2(10,0)
    18.             }
    19.         };
     
    San_Holo likes this.
  5. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    In the end the code below was reasonably ideal for what I wanted to do.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelGen : MonoBehaviour
    5. {      
    6.         public GameObject[] wavePool;
    7.         public float minX;
    8.         public float maxX;
    9.         public float minY;
    10.         public int waveAmount;
    11.         public float waveFrequency;
    12.         private GameObject wave;
    13.         public GameObject Boss;
    14.         Vector2 newPosition;
    15.    
    16.         void Start ()
    17.         {
    18.                 createLevel ();
    19.         }
    20.    
    21.         void createLevel ()
    22.         {
    23.                 for (int i = 0; waveAmount >= 1; i++) {
    24.                         wavePosition ();
    25.                         wave = wavePool [Random.Range (0, wavePool.Length)];
    26.                         Instantiate (wave, newPosition, transform.rotation);
    27.                         waveFrequency = waveFrequency + 5.0f;
    28.                         waveAmount--;
    29.                         if (waveAmount == 0) {
    30.                                 Boss.SetActive (false);
    31.                                 Instantiate (Boss, newPosition, transform.rotation);
    32.                         }
    33.                 }
    34.         }
    35.  
    36.         void wavePosition ()
    37.         {
    38.                 float posX = Random.Range (minX, maxX);
    39.                 float posY = minY + waveFrequency;
    40.                 newPosition = new Vector2 (posX, posY);
    41.        
     
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    yay stuff!