9

Here's one way to do it, using a custom struct to store object-weight pairs: public class Spawner : MonoBehaviour { // Here's where we define our weighted object structure, // and flag it Serializable so it can be edited in the Inspector. [System.Serializable] public struct Spawnable { public GameObject gameObject; public float ...


6

You want to slowly improve the strength, speed, and AI of these types of units. I'll provide a list of what exactly I would do in your situation: You can add where they do new moves once they hit a specific level. For example, they may shoot fireballs at level 10. Increase their reaction speed to your attacks. For example, they may have AI to predict when ...


5

From playing Knightmare Tower from beginning to end, it seemed like the enemies were mostly predefined as opposed to being procedurally generated. There was some randomization, but from what I can remember it was very minor variance. But to achieve something similar, I would do something along the lines of: Have several collections of enemies to pull from, ...


5

First of all please read Array versus List: When to use which? for coverage of this issue from a general perspective. Now to focus on your case, I would recommend a list if: You don't know how many enemies you will have in advance (and don't want to worry about handling resizing) You would like to remove enemies from the middle (and don't want to worry ...


5

This is a simple application of the distance-velocity-time triangle: (Image via BBC "Bitesize Maths") float timeBetweenSpawns = spaceBetweenBalls/ballSpeed; This relies on your units being consistent between the three variables. So if timeBetweenSpawns is measured in seconds then the spaceBetweenBalls should be measured in world units and the ballSpeed ...


4

If the blob has a constant vertical speed, and you know the horizontal speed of the incomming objects, you can use those variables to calculate the minimum and maximum angles the blob can travel to at most in that time frame. If you then calculate the distance between every two waves of obstacles, you can use that to find an angle that the player can ...


4

I'd go with something like this (pseudo-code): spawnProbability = 0.001 // Your original probability at fixed timestep spawnAtThisFrequency = 1/30 // The original fixed timestep dt dtAcc = 0 // An accumulator that will accumulate the variable dt update( dt ) { dtAcc += dt; // Accumulate while( dtAcc >= spawnAtThisFrequency ) ...


4

Don't trigger based on the camera's position. Trigger based on the player's. Once the player enters the trigger you spawn the enemies just outside the edges of the camera's view. Since the camera should be following the player anyway, you can derive the edges of the screen from its position and the screen dimensions. Then it's a simple matter of adding/...


3

You could use a for loop and a random integer between 1 and 4. using System.Collections; using System.Collections.Generic; using UnityEngine; public class CoinSpaws : MonoBehaviour { public GameObject coins; public float maxPos = 1.7f; public float delayTimer; float timer; void Start() { timer = delayTimer; } void ...


3

This falls under the Single Responsibility Principle (see SOLID). Specifically: the spawn point should handle entity creation, rather than a manager you would want to either have a field on the spawn point or a parameter on the spawn method that would tell it what to spawn you would then use an ordinary trigger behavior (maybe called TriggerSpawn, with a ...


3

One interesting solution might be to first draw a line which you want the player to follow then draw obstacles between random point in space above and bellow the line. You can then tweak difficulty by how many obstacles are spawned the with of this line and how complex the curvature of the line is. Easiest to get this line right is to first generate a set ...


3

In your update, you describe placing the walls on either side of a room. From your description and diagram, you intend to use random heights for the walls. Instead, I would recommend randomly deciding if the wall is going to be placed. Consider your diagram. In the diagram, you have walls of varying height; but if you consider each segment to be a wall, it ...


3

I would suggest creating a coroutine which takes three arguments, one for the gameobject which is to be instantiated, another for the number of times it should be instantiated, and the final parameter should be the amount of time between spawns. Psuedocode public IEnumerator InstantiateRandomCoroutine(Gameobject obj, int repeat, float cooldown) { for ...


2

OnBecameVisible and its counterpart has a few gotchas. It works only if the gameobject has geometry. The editor view also triggers these events. You might want to look at the CullingGroup API if you want to avoid these issues.


2

You can call Destroy with a 2nd parameter indicating the delay before the destroy occurs : public void SpawnObject () { if (GMS.counterDownDone == true) { Destroy( Instantiate (spawnee, transform.position, transform.rotation), 3 ); } if (stopSpawning) { CancelInvoke ("SpawnObject"); } }


2

In genetic programming, this is called the roulette selection function/algorithm. Here's one way to implement it: int selectFromRoulette(double[] weight, Random rng) { double total = 0; double amount = rng.nextDouble(); for(int a=0; a<weight.length; a++){ total += weight[a]; if(amount <= total){ return a; ...


2

First, you should combine this information: public GameObject[] objectsToRespawn; public List<Vector3> respawnPositions = new List<Vector3>(); public List<float> respawnTimes = new List<float>(); Nothing about these lists says that the indexes for each refers to the same index in the other (not to mention that one of them is a ...


2

Edit: whoops! I had the idea that I wanted the prefab of the object itself to remember which prefab it's spawning from, but neglected a wrinkle of prefab spawning: when you spawn a copy of an object, all references that object has to itself and its own components/children get translated in the copy to point at the copy itself and the copy's components/...


2

When you need to persist the state of a chunk but you are running out of memory, consider persisting it to the hard drive. If your game has enough complexity and data to warrant it, you might want to consider to use an embedded SQL database like SQLite for this purpose. However, I doubt that this is even necessary for your game. How much data do you really ...


2

Would simple for loop work? public GameObject[] Enemies; public Transform[] SpawnPoints; void KillEnemy() { for(var i; i < Enemies.Length; i++) { var spawnPoint = SpawnPoints.Length> i ? SpawnPoints[i] : SpawnPoints[SpawnPoints.Length- 1]; Instantiate(Enemies[i], spawnPoint.position, spawnPoint.rotation) } Destroy(...


1

The problem with arrays is that they can not resize dynamically. When you exceed the initial capacity, you need to allocate a new array and copy the whole array over. When you want to remove an entry in the middle, you need to move all entries after it down by one. These are both very expensive operations when your array is large. The List class is an ...


1

It looks like you want something like this: public class Population : MonoBehaviour { // Assign this Inspector variable to point at your prefab. public CarMovement carPrefab; public int populationSize = 10; // The Population class does the spawning now, so it can hold the spawn position. public Vector2 spawnPosition = new Vector2(0....


1

This example might be what you are looking for : Ray myRay; // initializing the ray RaycastHit hit; // initializing the raycasthit public GameObject objectToinstantiate; void Update () { myRay = Camera.main.ScreenPointToRay (Input.mousePosition); // telling my ray variable that the ray will go from the center of // my main camera to my mouse (...


1

When you spawn the element, set the isDragging to true. using UnityEngine; using UnityEngine.EventSystems; public class Draggable : MonoBehaviour , IPointerUpHandler, IBeginDragHandler { public bool isDragging; void Update () { if (!isDragging) return; // Put you dragging logic here } public void OnBeginDrag(...


1

for (int i = 0; true; i++) This is dangerous, because you risk looping in that piece of code forever, and thus freezing the game/editor. I am not sure how to change your code to make it fit within your game, but I would definitely put a limit to it like this: for (int i = 0; i < 100; i++)


1

I believe I have figured this out by hit and miss. The answer was that there was an (accidental) camera attached to my Terrain. I have no idea how that camera became a child of Terrain (almost certainly n00bie operator error), but that's where it was hiding. With 2 cameras in the scene, the behaviour of FPS was inconsistent and bizarre; I found that ...


1

You can pick a random location an exact distance away from the spawn point, first pick a random angle, find the x & y components of that angle, scale out by the desired distance & add the results to the spawn point to get the new coord: double spawnDistance = 10; Point.Double spawnPoint = new Point.Double(15, 17); Random rng = new Random(); double ...


1

In your code all spawn times are the same. If they can be different, things get more complicated, but I assume, that is what you are going for. Unfortunatley it seems to be impossible to pass parameters to the InvokeRepeating method (Unity Answers). So we do not use this method and handle the timed spawning on our own: public bool fromTop = false; public ...


1

It's because your have single enemy assigned to instantiate. You need an array of enemies to be spawned. public GameObject[] enemy; Then when you instantiate, either do it at random or as you need. Instantiate(enemy[Random.Range(0, enemy.Length)], //will choose from enemy array spawnPoints[spawnPointIndex].position, spawnPoints[...


Only top voted, non community-wiki answers of a minimum length are eligible