Showing posts with label FPS. Show all posts
Showing posts with label FPS. Show all posts

Wednesday, 21 October 2015

Fading Bullet holes

When I was playing around with the bullet holes, something I definitely wanted to do was cause them to fade out over time rather than just disappear immediately. The effect is slow and subtle so won't pull people out of the immersion of the game unlike constantly destroying the texture instantly would. After a bit I research I discovered that textures have an alpha setting along with the RGB settings. Alpha is the transparency of the texture, so in order to get my bullet hole texture to fade I needed to adjust the alpha setting over time. The way I thought of doing this was to use the interpolation mechanic I'd been taught the other week, to slowly adjust the setting from max to 0 over the course of three seconds.
Fading alpha interpolation code

After figuring all of this out and getting it to work I added it to my prototype scene to show it off.
Bullet hole partially faded

Monday, 19 October 2015

Pendulums and More Shooting

Continuing on from last weeks work about shooting, we added a few modifications to the scene. The first addition was adding a cross hair so the player could see where they were going to hit when firing. Using the new Unity 5 UI system makes adding text and images to the UI incredibly easy. Rather than having to code the entire object in a script, you can add a canvas to the scene and place an object upon that canvas. For images just move it into the position you want and for text items, do the same them add the text in and even stylise it if needs be. So I placed the cross hair image dead center of the screen, adjusting my bullet spawner to fire at the same point upon clicking.
Cross hair in scene used to destroy block

The second thing added to my shooter this week was a bullet hole upon impact. In order to do this I had to use Ray Casts for the game to judge on whether an object would be hit upon firing. If this is found to be true then a bullet hole will be instantiated at the point of impact giving a more realistic affect to the scenery. To stop graphical glitches, the bullet hole is not applied directly onto the object it hits, but set as a quad 0.01 units in front of the collision point. This stops the created bullet hole and target object from fighting for the same world space and flickering when looked at by the player.
Bullet hole created by bullet collision

When this was all implemented however, I very quickly found that the holes stacked upon one another. On top of this, since they were just placed where there was a collision, if the object the bullet hits gets moved then the bullet hole is left floating in space. To solve the first problem I needed to destroy the bullet holes after a little while to stop them from filling up space, so created a script which waited for 3 seconds after being created, before destroying it. To stop floating holes I had to set the object getting hit as a parent of the bullet hole. This means wherever the object hit goes, the bullet hole will go with it.
Code to create bullet hole and set object collided with as parent

After we finished with the new shooting mechanics, we moved onto something new: pendulums. Making a pendulum was surprisingly quick and easy, since all you need to add to an object is a Rigidbody and a hinge joint. When applying the hinge joint you add the object that will be the connected body and that causes the two objects to act as a hinge. Adding multiple objects in a row and hinging each to the previous one creates a chain that we added a sphere to the end of. After creating a wrecking ball it only made sense to add a wall to knock down with it, which is exactly what I did.
Pendulums swinging to destroy wall

Monday, 12 October 2015

Angry Birds and First Person Shooters



In week three we started doing something I've been looking forward to: first person shooters. Whilst we covered an Angry Birds style catapult system as well, the FPS mechanics are what I definitely want to use in my game, so learning about it helped me broaden my ideas for the mechanics I could use. We started the catapult/trajectory mechanic first, so I'll cover that to begin with. The objective was to create an object that, when the mouse was clicked and held, produced dots to show the trajectory of the object. Upon releasing the mouse button the object would be propelled along that path shown. In order to produce the trajectory, we needed to use two equations which calculated the distance the object traveled in both the x & y axis and project the dots along this path.
Trajectory equation code

For the first person shooter project we had to put a first person character controller into the scene and to that we attached a bullet script. This script very simply creates a bullet game object and applies an impulse force, which uses the objects mass and gives realism in the trajectory of the bullet. Initially the code was set to fire on every mouse click, which produces a semi-automatic gun. I changed mine to fire when the mouse button is down which creates a fully automatic gun, albeit an incredibly fast one which I'll need to tone down. To stop the scene from filing with useless bullets after they're fired, the bullets are set to be destroyed after 3 seconds. A pool manager would be far more useful and efficient so I'll work on that in my spare time.
Bullet fire code

In order to make the bullets fire from an appropriate position on the player, a gun script is needed which sets the firing position of the bullets to a specific place. In my case I used an empty game object attached to the right side of the player. This makes it look as if the player is firing a gun in their hands. For fun I added a second bullet spawn to the other side of the player to appear like the player is dual wielding the guns. These scripts are something I'll be playing around with a lot in order to get the best feel for my guns in my game.
Shooting block tower