24/02/23 - Unity Work
In today's lesson, we were working in Unity Engine again.
To begin the lesson, we first had to download some assets into our world documents, to use within the lesson, including a gun, gunshot sound and some grass/rock images. We then had to take the gun asset, and place it into the hierarchy, as a child of our Main Camera. We then had to alter the size and position of this gun, so that it looked like our character is holding it in place.We then had to create a new C# code script for this gun, within our Scripts folder, named Pistol Logic so that we could make it playable. Once we had written out this code, we had to drag it into the Inspector settings of our Gun option. We changed the features of this setting, by changing the Range to be 100, and the Damage to be 10. We also then had to drag in our Main Camera tab into the FPS Camera option, to connect them together.
After this, we had to create a target to hit. To do this, we created a cube within the hierarchy, and made it have a health of 100, so that it would take ten hits of our gun (which gives 10 damage per hit), to be destroyed. For this cube, we then had to create new C# code, named Target, so that it would be affected when we try to interact with it within the gameplay.
After tis, we once again returned to our Pistol Logic code, to add in the code for when our guns would next fire after being initially shot. To do this, we had to write in some code, to edit the rpm of our gun. We then went back to the game screen, and within the Pistol Logic script section, a new option for RPM had appeared, so this value, we had to change to be somewhere between 0.1 and 1, so I made mine 0.8, meaning that after shooting, I need to wait 0.8 seconds before I can shoot again.
Within the Inspector section of our gun, we then had to add in a new Audio Source component, and to this, we dragged in our gun sound asset that we downloaded earlier on. We then had to edit our Pistol Logic code to change the pitch of this sound, so that it would be random within certain parameters, to make it sound more realistic. Once edited, we went back to our game screen, and drag in our Gun tab from the hierarchy into the Pistol Logic scrip settings that we have, which connected everything together, so that when being played, the gun makes a shooting noise, when shot.We then wanted to make it so that our gun didn't clip through our surrounding objects anymore, so to do this, we firstly added a new camera into our hierarchy, as a child of our original Camera, and we named this GunCam, so that we knew which one it was. We then removed the Audio Source component from this camera, as it wasn't needed, and at the top, within the layers option, we added in a new layer, which we named Weapon. On the Culling Mask option, we then made it so that only this new layer was selected, and that the Clear Flags option was on Depth Only, with a Depth level of 80. We also then applied this layer change to our gun, so that it was connected, and this allowed the gun to no longer clip through other items.
We then wanted to be able to add in a score bar to our game, so to do this, we first added in a new Text tab in our hierarchy, and named this tab Score, so that it was easily findable. We then went into 2D mode, so that we could more easily see our text, and edited our text font size to be more visible. We also changed the placement of the text, to be in the top right-hand corner, by holding down the Alt button, and selecting the placement from the grid. I also changed my text to be 0, so that it was a baseline score option. We then returned to our Play mode, and saw that it had appeared on our screen where we placed it.We then had to create a new C# code within our Scripts Folder, named Score, which made it so that when we destroy our target, it will change our score text to whatever point value was applied to the target. We then dragged this code folder into the Inspection options for our Canvas, and then moved the Score tab from our hierarchy into the Score Text box that is available to change. We then had to add in a score value to our cube, which I chose a value of 10 for, so that when it is destroyed within the gameplay, the score text will change to be 10, from 0, when destroyed.
Finally, we wanted to add in a small shape, so that we know exactly where our gun was going to hit when playing on our games, as before this we didn't exactly know and would sometimes miss our target. So to do this, we added in a new UI Image folder into our hierarchy, which we then moved within our canvas, to the centre of the screen, and changed it's shape to be a circle, which we sized down and changed the colour of, to personalize it, which when we then returned to our gameplay, showed us exactly where we were aiming with our guns.
After we had finished all of our work for the day, we went onto GitHub to learn how to upload our work so that we can edit it along with other people.











Comments
Post a Comment