PRODUCTION - WEEK SIX

Monday 8th - Sunday 14th of April

For week six, I aim to continue the development of my mechanics, carrying on from where I left last week. As I got less done last week, I aim to catch up a small amount to get a timing projection. I will continue to work on the shooting, trying to get a better base. I will work on adding the reload function to the guns and continue development. For this week I said I would work on adding the features to the base projects, but I will hold this off till a further week to allow time for the team to all move onto production.


For the grenades and flash bangs, I will try to implement the basic throwing system into unity. This could be hard to get aligned with the Unity multiplayer scene, but I will attempt that when it is time to add these to the base project. I will work on letting the player throw grenades and flash bangs, then if time I will add the grenade explosion mechanics. For these, I will be using the grenade system in the bibliography.

Throwable Basics:

Video One:

To start with the grenades I wanted to do some basic implementation of the initial item system. To get this to work I started by creating the throwable info script and duplication the gun item script code. To this script, I changed the line from fps create a new gun instance to create a new throwable instance. This will allow me to create instances for each throwing weapon. This means if we were to ever scale this game we could add infinite new throwable items using this. From this, I create the two instances for the grenade and flash bang.


Next for the grenade system, I wanted to begin adding the variables starting with a header. The header allows me to keep order in the script aiding in understanding. I start with a reference header where I move my old references from the shooting script. For the grenade, I add a reference transform for the point of throwing and the player camera. The player's camera reference will allow the script to throw a grenade no matter where the player is looking in the scene. the throwing transform will determine the area the grenade is thrown from and will follow the camera. Then to the references, I add a game object reference which will determine the object thrown. Later it may be possible to set this to the game object determined on the item script.


Next, I move on to the first adding a header for the integers and floats. to start I create an integer variable named totalThrows to keep count of every throw. This will help the script keep track of the players' throws and will help reload the player's grenades. To keep track of the cooldown I decided to implement a cooldown period between each throw. This way, I can control the pace at which my grenade can be thrown. Next, I add the force for the grenade adding one for the forward direction and one for the upward direction. Lastly, for these variables, I add a bool to control whether they can throw the grenade or not then add a key using a keycode to determine how the player will throw the grenades.


Video Two & Three:

Next on the script, I worked on adding the throwing function to the code. First, I add the ready-to-throw bool and set it to true in the start function. Then in the update, I start performing the input check. For this I first check for the input using the input get key, after that, I check if the ready-to-throw bool is true and if the total throws are higher than zero. This stops the player from throwing grenades unless they have grenades and are not on a cooldown. Then, if this is all true, the update calls the throw function. 


In the throw function, I set the ready-to-throw to false to not let the player throw again. Then I instantiate the object which will take in the game object I set in the variables. For the position, I set it to the throw points position, then set its rotation to the player's camera rotation. After this, I create a new rigidbody and set it to the rigid body of the instantiated object. This is so I can add the force to the original rigidbody. Next, I move on to calculate the force need to add. Here I add a vector three which will hold the force. For the force calculation, I first get the forward vector from the player camera and times it by the forward force. Then I get the up direction from the object and times it by the upward force.


Using the force, I add force to the rigid body of the objects using force mode impulse. Then I minus the total throws to remove one from the ammo count. Then at the end of the throw function I invoke the reset throws function using the cooldown to only call it after the cooldown is complete. This ensures every time the throw is called, it waits until it should be reset. Lastly, to finish off this part of the code, I added the reset function and made the ready-to-throw bool true to reset the player's throw.


Video Four:

For the next part, after I coded the basic grenade system, I decided to move on to adding the grenade object to the scene. For this I will follow the same hierarchy as the gun items, having an item to hold the script, one to hold the body and one for the actual object. I added a sphere to be the stand-in for the grenade. To the script holding object titles grenade item, I add the grenade item script and move it into the item holder. 


I spent some time messing with the object, adding the rigid body. I drag the object into the assets to make a prefab which is well-used on the grenade objects. Then I begin filling the grenade controller variables, adding in the player camera and fire point transforms. I set the total throws and cool down and mes with the force. The force will be changing as I test with the team later.


Video Five:

After adding the code for the throwing, I had to make sure the throwing aimed for the centre where the player aimed. This is because the throwing point is from the hand and misses the centre. To fix this, I will implement the ray cast aim feature. I added a vector and set it to the camera direction used earlier. I then created a ray cast hit called hit. Then I check if a ray cast hit is detected, for this, I use the position of the camera and the camera's forward direction. I take in the git and send it out 500 on the length. If this is the case, I change the force direction vector to the hit ray cast point minus the throw point position, and I make sure to normalize this. This calculation finds the difference and makes it up for the trow. Then in the add force, I use the new force addition instead. Lastly, to fix this on my version, I am adding some code from the comments.


To fix this problem, I have to fix the initial rotation of the instantiated object. To do this, I will add a quaternion, which is how unity handles rotations. While they are complicated, I will continue following the comment. First, I create a rotation quaternion and set it to a from-to quaternion, taking in the forward direction vector and the forward direction of the transform. I then take the throw object rotation minus the new quaternion and the rotation of the throw point. Lastly, I set the new instantiated rotation to the new quaternion rotation.


I included an example of the grenade system at the end

Video One:

Video Two:

Video Three:

Video Four:

Video Five:

Grenade Example

Flashbang:

Video One:

To start on creating the flashbang, I first need to add an object to handle the object and scripts. First, I add an empty object to the hierarchy and add a child to it. The first one will be the flash-bang item and the second will hold the mesh. For the mesh, I will add a temporary cylinder, which will later be replaced with the finished mesh. I scale this mesh and move it in an attempt to get it to the correct size and place. While I was doing this I noticed weird scaling issues which I needed to fix. Which I fixed by resetting the scale on all objects in the item holder. This happened because one of the empty holder items had an uneven scale (for example, 1, 1, 0.8).


Next, for the grenade, I added a rigidbody to the mesh so it would be calculated using physics. This is mandatory to get the grenade working in the game. To the item object, I add the item script and the flash throwables instance to it. I create a prefab using the mesh and add the explosion script to it. Then, lastly, I add the item to the list of held items.


Video Two:

Now to the main explosion script, I will add the beginnings of the code that will handle the flashbang. To start, I added an is-flash boolean, which will be called anytime the flash bang goes off. I add this to the collision enter code, where if true I invoke the flash function. In the function, I copy the Invoke destroy projectile function from the grenade. This ensures the flash bang is destroyed when it goes off. I added a debug to the script to ensure I could tell if the flash bang went off. 


After this, I attempted to make it, so I could have multiple objects set to be thrown. This would allow me to have any item in an array that used to be a throwable. I set the thrown object to an array and tried to set the array to the currently held item integer. Unfortunately, this did not work because the held item had more options than the thrown objects. In the future, I need to make it so the throwables are determined by the throwables being held, but I could get it to work this way. Instead of this, I just used an integer set by me in the inspector and used that in the array.


Video Three & Example:

Next, I am going to begin adding the code for the flash. I start by adding the code to control the time using two separate integers, one for the grenade and one for the flash bang. I then make sure in the code for calling the explosion both systems take in their respective times. Then, to sort my code a bit more, I created a new header for the references to sort them. This not only makes my code easier to read and understand but ensures it is optimized for the Unity system. Now I will move on to making the check visibility function. This will be called when a flash bang goes off to test if the player is looking at the flashbang. 


To start following along with my research, I will start with the basics and expand later. So to start I set two variables, one for the planes and one for the points. I calculate the planes using Unity geometry utility calling it calculate frustum planes function taking in the camera. Geometry utility in unity is used to calculate geometric functions, with the frustum plane simply being the camera view space in 3D. For the points, I just set it to the position of the transform. Then, I use a for each to check every view instance in planes. This will check for any camera looking at the flashbang. In the for each loop, I return the distance to the point. Then outside this function, I return false.


Now in the flash function, I use an if statement to check if check visibility is called, then if called I debug go blind and if not I debug do not get affected. This will show me if the code is working when the player is looking at it. It will also make it, so I can ensure it is not always blinding the player and only if they are looking at it. You can see this working in the video, but a complete example is in the example video.


Lastly, for this part, I will add the code to handle the view disturbance code. This part will make sure that if an object obscures the view, then the player will not get affected by the flashbang. To start this, I changed the get distance code to an if statement. This allows me to add a different outcome, depending on the situation. Then in the statement, I add a ray and set it to a new ray. In this ray, I set the start point at the camera position, ensuring the ray is sent from the player's camera. Then for the end distance, I set it to the position of the explosion and take away the transform of the camera. After this, I created a new hit. Both of these will be used in the ray cast. For the ray cast, I check if the ray cast hits using an if statement and the ray and hit. If this is the case, then the code returns the hit Gameobject equal to this Gameobject. If the point distance is not true the n the code returns false. 


Video Four:

To begin creating the flash effect, I added a canvas and a panel, which I set to white. I call this the blind effect canvas, as it controls the effect. For the effect, I added a post-processing volume to the canvas and post-exposure. I set this to ten, adding a blinding effect to the level. I animate this to slowly fade out and animate the panel to appear. This panel will hold a copy of the player's camera image giving the after-image effect. I set up the animator making sure there is a trigger. This code in the script will be called when the flash goes off. In the animator, I set it so that from the entry state and any state it can go to the flash effect when triggered. This will allow me in the script to call the trigger to start the animation.    


Video Five & Final Example:

Lastly, for the flash bang, I will incorporate the flash effect into the script.  For the script, I added a separate script for the flash blindness effect. To this script, I add a variable for an image, which will be the image the screen is imprinted on. This will be the main aspect of the after-image effect, as the currently seen screen will be imprinted on the image and overlaid on the panel. I added an animator reference, so I can call the animation I made before. For that panel, I added a reference where the image will be overlaid. I created two integers to hold the height and width of the image to be overlaid. Most of these will be found in the start function, where I set the width and height to the screen's width and height. 


Next, I added the functions for the code that will handle the blind effect. I added a go-blind function and a go-blind coroutine, which I call the go-blind function. Then in the coroutine, I start by yielding a new wait for the end of frame, which will determine the grab point. Then I create a new texture 2D which will hold the image grabbed. This allows the code to overlay an image, giving the after-image effect. To the texture, I set the scale to the height and width, setting the format to RGB24. Next, I call the read pixels function from the texture, using a new rect of 0 on the first values and width and height on the last two. After this, I added a new vector setting to 0.5 on both values. Lastly, I set the trigger in the function.


Lastly, to get this effect to call, I have to implement it in the explosion function on the other script. I do this by adding a static blind effect called Instance, which I can call on the other script. Then over in the other script I set a reference to the blind effect and call its active instance in the flash function calling back to the go blind function.


The final flash bang effect can be seen in the example video. 

Video One!

Video Two!

Video Three!

Debug Flashbang Example!

Video Four!

Video Five!

Flash Bang Example!

Reflection

Grenade - Reflection:

To reflect on this, I am happy with getting the underlying grenade system working with only the throwing setting made. To improve, I will have to incorporate it into the pre-existing system I have made for the weapons. I have added the basics for this, but have not managed to add any customization for each weapon of this type. Once I add this, it will make it easier to make the flash bang and incorporate it into the system. This will be one of the tasks for week seven. I am happy with the customization that can happen as each aspect of the grenade throw can be changed as seen in the final demonstration. I will also aim to finally add the explosion effects during week seven.


Flash Bang- Reflection:

To reflect on the flash bang, I am happy with the outcome, as the video I chose created an incredible effect for the flashbang. My only issue with this flashbang code is that the player has no range on where the flash has no effect. The lack of range means no matter the distance, the player will still be affected. Still, the script has code to handle objects in front and the handle-looking directions. This code should be good for our project. In the future, features like the timer and flash effect could change as they are more for testing.