Week 10
Created Basic Item Spawning System W/ Instantiate
Created a C# script Named ObjectSpawner. Using a Public Transform ‘Spawnpoint’ and public GameObject ‘Prefab’, with a void OnTriggerEnter, I Instantiate the prefab, Spawn point position and spawn point location. Back in the game scene I created a cube to use as a trigger, so I named it ItemSpawnTrigger and in the box collider I checked is Trigger, I then turned off the mesh renderer so the player character can walk through and trigger it. I then added the ObjectSpawner script to the Trigger box, then in the inspector I just added a coloured cube prefab for testing. In the hierarchy I created an empty game object to be used as the spawn location indicator, I set the indercator where I wanted the item to spawn and then made sure to add the SpawnLocationIndicator within the inspector of the Trigger.
Tutorial used: https://www.youtube.com/watch?v=_Xrw2EEhzI4&t=267s
Created an RNG Loot Pool System (Not yet implemented to have weapons spawning)
Created a C# script named RNGLootPool. To set up the loot chance percentages I used an array. Using a Public Int [] naming it RNG chance and having it equal to the array, which for testing I set up as three items one at a 60% one at 30% and the last at 10% chance to spawn. I then created a public List<GameObject> Cubes. Then made a public int Total to tally the total, to do this I used a foreach (var item in table) {total += item} this adds up the total.
I then made a public int randomNumber to ‘roll a die’ and pick a random number. To do this I used randomNumber = Random.Range (0, total). Now that I have the random number being made I created a foreach loop to iterate through the table and compare the random number to each index (the spawnable items chance). This foreach (var weight in table) is used to compare my random number and see if it is <= to the current weight and if it is then spawn item, if it isn’t it’ll make its way up the list until it is, for e.g. the current loot item chances are 10% 30% 60% if the random number is 45 then it checks if it is <= 10% when it sees it’s not it moves up one to 30% then finally to 60% and because it is <= to 60% is spawns the item tied to that index. Although if the random number in this test was 80 then it makes its way up the line like normal but once it gets to 60% and can’t go any further but it’s still not <= to the current weight (60%) so it’ll have to lower itself until it is, the way it does that is by using an Else {randomNumber -= weight} this will subtract the current randomNumber by the current weight, so for this example 80-60= 20% having this number now the system can see that it is <=30% and will spawn that item in.
Tutorial used: https://www.youtube.com/watch?v=OUlxP4rZap0
Leave a comment
Log in with itch.io to leave a comment.