..continued from Fortnite Chest Simulator Part 1.

Moving on to the visuals for the chest simulator I knew there were a few things I wanted to achieve. The first being that I wanted to make sure there was a graphical representation of the differences of choice that the generator made. I also wanted to have it randomise the placement of the loot it was distributing and finally I did not want to use animation.

The animation thing was purely a decision made on the fact that I detest doing animation, its not that I find it hard or difficult but more time consuming and sometimes it takes a lot to get a simple results. I also did not want to try and engineer an animation system that dealt with the randomness I wanted to achieve. There was also the added extra consideration that the chests might not be placed on perfectly flat ground so at some point, even if I did use animations, physics was going to have to take over.

Figure 1: Idea generation

So looking back on my prototyping stage I had already made a few decisions, one was to find an asset pack to use for the graphical representations, and another was to look at explosive impulses in Unity. So I started with the asset pack investigations.

I actually have a project that just has all the asset packs I own or have obtained through the last couple of years, it functions like a library for me so I can quickly find and visualise what I am looking for. Now obviously I needed lots and lots of guns and for once my asset library did not come through for me, I just did not have the abundance of guns I needed. So I went looking in my usual places, itch.io, Kenney Assets and the Synty Store. Now there are plenty of assets here but as I was browsing through, trying to collate what I could use I had a small brainwave.

I already had the data I needed to decide what was being pulled out of the chest, and I was storing this as a string. So why could I not just utilise this in some way within the game and create an object I could attached physics to? Obviously in order to do this I would have to utilise the UI Canvas aspect of Unity, but I would need to put the canvas into the world space. When I did this however the text became really poorly rendered so I did some research and found this video from when the World Space canvas was added in to the engine.

Starting from 2 minutes in there is some wonderful advice and tips about positioning and scaling the world space canvas, the key aspect here being scaling the canvas rather than resizing. This allows for the contents of the canvas to retain their original scale, meaning that text does not become an unmanageable 1px size.

Now that I had readable text I just needed to give it physics, I knew it would not be a good idea to give the canvas itself physics so I decide to place the canvas inside an empty game object and then add the physics to this. This resulted in the following

Figure 2: Item prefab in engine

Now I had a game object that I could manipulate with physics and that contained a world space canvas that I could change the text aspect of to the string name of the chosen weapon. As you can see from the image above I also put a light into the object, this was precautionary just in case I need to change the colour of something at a later time. I actually did end up using this light in the code, along with changing the colour of the text dependent on its role, rarity and type.

I created a prefab of this item and added it to the Chest Manager script as a game object I could instantiate. This was the next thing to tackle, how to spawn these items in but I realised it would be best to work out how to move them around before I tried to get multiple versions of them spawning in at once.

This is where the fun begins. I first attempted to just use a regular AddForce method to the rigidbody where I randomised the Vector3 position in the world coordinates around a central point. This would mean that all the objects would spawn in the same place and then I would pick a point just outside this point, at random, to add the force from and throw the objects out of the chest.

Well it worked but it was extremely buggy. I had objects colliding with each other and shooting off in every direction, including out of view. I had objects all going the same way due to the randomness of it. Basically I was not happy with the result. So I went back to my original prototype idea of AddExplosiveForce.

This method simulates an explosion at a point and its effect on the object you are telling it to interact with. So if for example you had an object at x = 1, y = 1 and z = 1 and you applied explosive force to it from x = 0, y = 0 and z = 0 this would be as if the explosion had happened just below the object and offset from its own center point. So it would throw it off in direction up in to the air.

Now this was perfect for what I wanted but I still had two problems. One the objects were all spawning in the same point so therefore any force I add to them would have the same effect on all objects, and two, it would be the same every time I ran it.

The solution for this was quite easy. First I create 4 spawnpoints around the centre point of the chest, I would step through an array of these spawnpoint and spawn an object on each one. Next was to put all of the spawnpoints into a parent object that was position where I was going to simulate the explosion force.

Figure 3: Spawnpoints in world space

What this allowed me to do was spawn in the 4 objects that I needed and push them all off in different directions as this is how the explosion would add force to them. Then to create an extra level of randomness I changed the rotation of the GameObject at random, as the spawnpoints are children of this object that meant that they would change position every time my function ran. This meant that I had managed to achieve my goal of not using animation and also ejecting the items in random directions every time it ran.

Below is the code I used to rotate the main GameObject and also the script I ran on each instantiated object in order to throw it out of the chest.

One thing I did have to do in order to make this work was considerably shrink the size of the BoxCollider that is attached to the instantiated objects. This was just to ensure they did not collide with each other.

Next was to compile it all together into a working example that allowed for the objects to be decided, then to had the objects change colour dependent on the decision, then to spawn the items, then to throw them out.

This is what I ended up with.

So this script above combined with the ForcePush script further up this post produced my final result, it looked like this.

Figure 4: The final prototype

So now I had two versions of my prototype, one that just handled the mathematics and another that used these mathematics to feed into a visual representation.

Overall I am really happy with what I created, its not an exact recreation but it has given me an insight into the thought processes that were probably required in the actual creation of this mechanic.

Oh and you can play this version of the simulator here https://defconsoft.itch.io/fortnite-chest-simulator-2

References

itch.io. 2021. Top free game assets. [online] Available at: <https://itch.io/game-assets/free> [Accessed 15 June 2021].

Kenney.nl. 2021. Kenney • Home. [online] Available at: <https://www.kenney.nl/> [Accessed 15 June 2021].

Synty Store. 2021. Synty Store – 3D Art for games (Unity + Unreal). [online] Available at: <https://syntystore.com/> [Accessed 15 June 2021].

Youtube.com. 2021. Unity 4.6 – New UI World Space Canvas. [online] Available at: <https://www.youtube.com/watch?v=Mzt1rEEdeOI> [Accessed 15 June 2021].

Technologies, U., 2021. Unity – Scripting API: Rigidbody.AddExplosionForce. [online] Docs.unity3d.com. Available at: <https://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html> [Accessed 15 June 2021].

Figures

Feature Image: FortniteIntel, 2019. Fortnite chest graphic. [image] Available at: <https://fortniteintel.com/wp-content/uploads/2019/03/chest.jpg> [Accessed 19 June 2021].

Figure 1: Charlish, R (2021) ‘Idea Generation’ Created on 22nd June 2021

Figure 2: Charlish, R (2021) ‘Item prefab in inspector’ Created on 22nd June 2021

Figure 3: Charlish, R (2021) ‘Spawnpoints in world space’ Created on 22nd June 2021

Figure 4: Charlish, R (2021) ‘Final Prototype’ Created on 22nd June 2021


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *