├── .github └── FUNDING.yml ├── Assets ├── Prefabs.meta ├── Prefabs │ ├── Enemy Green.prefab │ ├── Enemy Green.prefab.meta │ ├── Enemy Red.prefab │ ├── Enemy Red.prefab.meta │ ├── Enemy Yellow.prefab │ └── Enemy Yellow.prefab.meta ├── Scripts.meta ├── Scripts │ ├── EnemySpawner.cs │ └── EnemySpawner.cs.meta ├── scene.unity └── scene.unity.meta ├── LICENCE └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ['https://paypal.me/hamzaherbou'] 2 | -------------------------------------------------------------------------------- /Assets/Prefabs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0ac6a71aaf6b85a4a9daefec4772a605 3 | folderAsset: yes 4 | timeCreated: 1630519242 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Prefabs/Enemy Green.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/Unity_SpawnerWithProbabilities/12104daca4853665ed417688e1139c9386f120b6/Assets/Prefabs/Enemy Green.prefab -------------------------------------------------------------------------------- /Assets/Prefabs/Enemy Green.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 61976a16569899246a880c0dc92e6d07 3 | timeCreated: 1630515892 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | mainObjectFileID: 100100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Prefabs/Enemy Red.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/Unity_SpawnerWithProbabilities/12104daca4853665ed417688e1139c9386f120b6/Assets/Prefabs/Enemy Red.prefab -------------------------------------------------------------------------------- /Assets/Prefabs/Enemy Red.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5c4abb664c213224089e5d03dd3ded92 3 | timeCreated: 1630515892 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | mainObjectFileID: 100100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Prefabs/Enemy Yellow.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/Unity_SpawnerWithProbabilities/12104daca4853665ed417688e1139c9386f120b6/Assets/Prefabs/Enemy Yellow.prefab -------------------------------------------------------------------------------- /Assets/Prefabs/Enemy Yellow.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 89b7e2d88a8dc0c448f47799cedabf46 3 | timeCreated: 1630515892 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | mainObjectFileID: 100100000 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f7fa73c744531a64c94226d78d71f92a 3 | folderAsset: yes 4 | timeCreated: 1630519235 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scripts/EnemySpawner.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine ; 2 | 3 | [System.Serializable] 4 | public class Enemy { 5 | // for debug : 6 | public string Name ; 7 | 8 | public GameObject Prefab ; 9 | [Range (0f, 100f)]public float Chance = 100f ; 10 | 11 | [HideInInspector] public double _weight ; 12 | } 13 | 14 | 15 | public class EnemySpawner : MonoBehaviour { 16 | [SerializeField] private Enemy[] enemies ; 17 | 18 | private double accumulatedWeights ; 19 | private System.Random rand = new System.Random () ; 20 | 21 | 22 | private void Awake () { 23 | CalculateWeights () ; 24 | } 25 | 26 | 27 | private void Start () { 28 | // Test: Spawn 200 enemy : 29 | for (int i = 0; i < 200; i++) 30 | SpawnRandomEnemy (new Vector2 (Random.Range (-3f, 3f), Random.Range (-4f, 4f))) ; 31 | } 32 | 33 | private void SpawnRandomEnemy (Vector2 position) { 34 | Enemy randomEnemy = enemies [ GetRandomEnemyIndex () ] ; 35 | 36 | Instantiate (randomEnemy.Prefab, position, Quaternion.identity, transform) ; 37 | 38 | // This line is not required (debug) : 39 | Debug.Log ("● Chance: " + randomEnemy.Chance + "%") ; 40 | } 41 | 42 | private int GetRandomEnemyIndex () { 43 | double r = rand.NextDouble () * accumulatedWeights ; 44 | 45 | for (int i = 0; i < enemies.Length; i++) 46 | if (enemies [ i ]._weight >= r) 47 | return i ; 48 | 49 | return 0 ; 50 | } 51 | 52 | private void CalculateWeights () { 53 | accumulatedWeights = 0f ; 54 | foreach (Enemy enemy in enemies) { 55 | accumulatedWeights += enemy.Chance ; 56 | enemy._weight = accumulatedWeights ; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Assets/Scripts/EnemySpawner.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3e9b4da1330daec45999d9fb2b088cac 3 | timeCreated: 1630519605 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/scene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/Unity_SpawnerWithProbabilities/12104daca4853665ed417688e1139c9386f120b6/Assets/scene.unity -------------------------------------------------------------------------------- /Assets/scene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ef803f5654e5e7b4299573f94e2702cd 3 | timeCreated: 1630253051 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Hamza Herbou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity Tutorial - Spawn random enemies with probabilities 2 | 3 | ### Video tutorial : [https://youtu.be/Hy6Gxtk0QwY](https://youtu.be/Hy6Gxtk0QwY) 4 | 5 | ![Video thumbnail](https://img.youtube.com/vi/Hy6Gxtk0QwY/0.jpg) 6 | 7 | 8 |

9 |
10 | ## ❤️ Donate 11 | Paypal 12 | --------------------------------------------------------------------------------