Monday, 22 October 2018

Game - Developing the shooting

In this post, I will be describing how I set up the shooting mechanic into my game.

In our assignment, we were asked to use the FPS template built into the Unreal engine, which gave us the basic shooting mechanic. The Blueprint class was set up so when the user presses the Fire key (from the InputAction Fire event), it would retrieve the camera's rotation and forward vector, and fire a projectile forward.

However, I have removed the projectile shooting, as a laser would not shoot an object - instead I would use a LineTraceByChannel. The LineTraceByChannel function is used to check for collisions based on two Vectors, and I will return the LineTrace hit with the BreakHitResult function. The Start Vector is the current gun's position, and the End Vector is the player's forward Vector (multiplied by 10,000).

With the BreakHitResult function, I can retrieve what object that the player aimed at. I compare the BreakHitResult output, to see if the component has a tag of 'Shootable'. If so, the object hit will receive damage (using the ApplyDamage function), play a shooting sound effect (using the PlaySoundAtLocation function), and add one to the score.

Score + 1 function
I wrote a small custom function to add a point to the score. I chose to write an independent function, as it lets me re-run this small step in numerous pieces of code. Score is an integer variable, and it uses the Addition method to add '1', and set the score.



Conclusion
The LineTraceByChannel function in Unreal could compared to the Raycast function in Unity. Both features allow the user to compare two Vectors, and check for any collisions. This seems to be one of the best methods for shooting, as it can give you an instant response for what object has been collided with.