Post news RSS Counter Sniper: Scripting Guns w/ Unity Visual Scripting

How I scripted the guns for Counter Sniper with Unity Visual Scripting.

Posted by on

Counter Sniper Gun Script

This is how I got the rifle to fire, and at specific times not fire, in Counter Sniper. Maybe a little more complex than it needs to be, but there aren’t a lot of visual scripting tutorials out there. So, I figured I would at least put this out there.

Art 3 Pic 2

The rifle is built to fire when the player presses down on the left mouse button, pretty common. From there, I do a check on a boolean variable called “Firing.” I’ll show where this variable is controlled later. In an early draft I didn’t include this gate and the rifle essentially became automatic. Coupled with the “If” gate, it just assures the rifle will not fire while it is already firing.

The next gate is similar, just checking if the integer that keeps track of ammo is above zero. Making sure the rifle cannot fire when it is empty.

Art 3 Pic 3

Which I of course immediately follow up with a controller subtracting the bullets being fired from the ammo reserve.

Art 3 Pic 4

Quick activation of an audio controller attached to the rifle to make a firing noise. Then it’s on to figure out where the bullets should actually be coming from. We move onto another boolean which keeps track of whether the player is zoomed in or not.

Art 3 Pic 5

“AimFire” is an empty game object attached to the rifle where I want the bullets to spawn where the player is zoomed in. There is an identical script attached to the “false” output of the “Aim Check” that just has “HipFire” switched out for “AimFire” and spawns bullets in the unaimed rifle barrel. If you hadn’t guessed, the commands for get position and get rotation just get world information about the “AimFire” point so the script knows where to instantiate the bullet and which direction it should be facing.

Art 3 Pic 6

This is the script where we get the forward of the player and add force to the newly spawned bullet. I have the multiplied force, or “B” set to a variable called Bullet_Speed_LowCal because I wanted to be able to change out the speed of bullets without going into this script every time to fiddle with numbers, but you could just as easily replace that block with a Vector 3 all set to “120.”

Art 3 Pic 7

The first block in this script is what finally adds the force we just went over to the bullet we’ve been talking about. Then we get back to the “Firing” boolean form the first picture. Up until this point with no other delays in the script, this has all been executed, more or less, instantly. So, having the “Firing” toggle here will allow the player to fire one bullet, and then switch on the variable that stops any further shots while the script waits for 1.5 seconds before finally switching itself off again and ending.

Art 3 Pic 8

There’s also a little script-piece for reloading.

Art 3 Pic 9

And then another that checks if the player is zoomed in or out by watching the controls.

All together, not all that complex, especially for more experienced programmers, but it was a nightmare trying to originally assemble this using old Bolt and Fungus tutorials and forum posts. Hopefully, this post can help other people just starting off with Unity's Visual Scripting.

Maybe because of all the suicides, Counter Sniper is still in Alpha, but if you want to hear more about my failures, please give me a follow on IndieDB for future articles and updates, join our Discord to ask questions or give suggestions, or follow me on Twitter because why not.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.