Tuesday, 13 December 2016

Week 8


 In the eighth week, we were introduced in the try-catch-finally in C#, an aspect that we covered in Python as well in week 7. A practical way to use "catch" and "finally" together is to obtain and use resources in a "try block", deal with exceptional circumstances in a "catch block", and release the resources in the "finally block". Also, I continued to work on the ranged enemy. After I imported the sprite sheet for the ranged enemy, I sliced it and I created the prefab, where I added some components like a box collider with the tag "BlockingLayer" in order to prevent the player from passing through the enemy and I added a rigid body where I froze the Z rotation to protect the ranged enemy from falling because by freezing the Z rotation is harder for the main character to get past the enemy, else he could easily push him back, make him fall and prevent the enemy from attacking.







 After I set up all the components of the inspector I focused my attention on the scripts. The thing that I wanted for the ranged enemy to do was to start shooting projectiles as soon as the player finds himself in a certain range. So the first thing which I did was to import a simple bullet from the internet:


After I found it and I imported it in Unity I dragged and dropped it on the game scene and I created a prefab. The first thing I did was to add a box collider (the box collider is triggered) and a rigid body (for the motion) to the bullet. The first I added to the bullet was the "Destroy" script because if the bullet is not hitting the target I wanted to make it disappear after a small amount of time. So I declared a public variable called "lifetime" which was set to 2 seconds, after this, I created an "Awake" method which has "Destroy" function in it which destroys the game object after a certain amount of time.




 The next script which I wrote was the script for the bullet controller. The first things which I did in the script was to declared some variables like: the speed of the bullet, the "playerController" (I need the player controller because I need to access the game object in order for the bullet to find and to give damage to the main character), a public integer for the damage, and finally the rigid body. The first to things declared in the "Start" method are the player because, as I said earlier the bullet needs to find the player, and the rigid body (essential for the motion). The if statement highlights that the speed of the bullet is determined by the position of the player. In the "Update" method, the first function sets the velocity of the bullet, the speed of the bullet and the direction (which is on the Y axis). The if statement underline that the bullet touches the "tag" (player), then the player's health should modify by decreasing it. The final function is for the bullet to get destroyed after it reaches its target. Another import piece of information is to set up the "Layer" of the bullet prefab as a projectile.







  



 The last script I created was for the ranged enemy to be able to shoot at the player when it enters in a certain range. The first thigs that I added to the script were: a "public float playerRange;" because in this way I can in what kind of range the player is, the second variable is the bullet, the third variable is the player's position, the "launchPoint" variable defines the point from where the bullet will launch also I added a child to the "Ranged Enemy" prefab to set the exact lauch location. The last 2 variables are defining the countdown timer which will control how many shots the ranged enemy has. Also, in the "Start" method I set the "shotCounter" equal with "waitBetweenShots", basically enemies will not be able to fire for however you set the "waitBetweenShots". In the "Update" method I will countdown the shots basically when the "shotCounter" will be less than 0 the ranged enemy will be able to fire. Also, what I wanted to do was to reset the "shotCounter" every time the ranged enemy is shooting so I added to "shotCounter = waitBetweenShots;" in both the if statements. In the "Update" method I used the "Debug.DrawLine" function because I wanted to draw a line to see the trajectory of the bullet. The two if statements inside the "Update" method are basically for the enemy to find if the player its either left or right. When the enemy will find the main character he will start shooting. In both of the if statements I have the "Instantiate" function which makes a copy of an object in a similar way to the "Duplicate" command in the editor. If the "GameObject" is cloned the position and the rotation will be specified (these are default from the original GameObject's position and rotation). Also, "Quaternion" is useful to represent rotation.




 What I need to improve at the ranged enemy: I have to work on the exact same feature as the melee enemy, in the original "game plan" the enemy was supposed to crouch when he was shooting. Also, the enemy was supposed to flip as well but I did not have time to finalise the flipping script for the ranged enemy. 



References: 



No comments:

Post a Comment