Final Project Week 15 - Bug Fixing and Input Manager


In my previous blog post, I finished all of the features that I planned to implement into my system. because of this, I took a break from this project for easter and to focus on my other university module. I did test my system over the break, in this blog post, I will be focusing mainly on fixing any issues that I found. I will also work on implementing some stretch goals that I wanted to implement if I had time.

Input Manager and Controller Support

The first stretch goal that I wanted to implement was an input manager that would make the system easier to set up with the keys the developer wants, I also used this time to ensure that my whole system is usable with a controller, in case the developer would like their game to be used on consoles. 

Input Manager

Unity already has an input control built in, my manager is not made to replace it, but to allow the user to select the unity input that they would like to use, without having to give their inputs specific names, or go into the code and change it themselves, this just allows the developer to set up the system exactly how they would like.

Custom Inspector for the Input Manager
To do this I made an input manager script that has 2 lists of inputs, Buttons and Axes, each input has a name, that is set by me and will not be changeable, and a string that the user will fill in with their input name. The input manager will create an instance of itself, meaning that any script can get input names from it. I wrote 2 functions that take strings for the manager that will allow other scripts to find an input without having to search through the list of inputs each time you want to get input.

Once the manager was complete, I wrote a custom inspector that limited what the developer could change. This is necessary because I want the system to be useable without touching the code. If the user has access to the names of the inputs in the input list, the scripts would throw errors because they wouldn't be able to find the inputs they need.

Controller Tweaks

With Unitys input system and my input manager in place, a lot of the weapon system was usable with a controller already, the items that needed changing are shooting, aiming and UI control.

The reason the majority of the inputs already worked is because the input method on a controller is the same as a keyboard and mouse, for example, movement and mouse look already used axes, so remapping that to the sticks worked perfectly, other inputs such as jump, switch weapon and open editor, all used buttons on both keyboard and controller.

The reason aim and shoot do not work in the same way is that using a mouse, they use buttons, but on a controller, they use the triggers which are treated like axes. Fixing this was fairly simple, in the code, there is an if statement that will check if the button is pressed, in that if statement I added an or that checks if the aim/shoot axes are over 0, this means that it is pressed. This worked perfectly for the aiming but didn't work as intended for the shooting, this is because the shooting system that I have set up looks for when a mouse button is pressed, not held, but there is no built-in function to check if an axis has been pressed. This meant that on single and burst fire, the gun would shoot constantly while the trigger is held, without waiting for it to be released. To fix this issue I created a function that will check if the trigger was pulled in that frame, and will then wait for the user to release the trigger before allowing the weapon to shoot again.

Code used to check if a trigger was pulled in this frame

This can be called very quickly wherever needed now, allowing me to set up single and burst fire with a controller.

The GitHub repository for this project is available here: https://github.com/ElliotChester/Modular-Weapon-System-Final-Project

Comments