Post news RSS Counter Sniper: Scripting the Distraction Cannon

How I scripted the Distraction Cannon and the Distraction Dummy for Counter Sniper.

Posted by on

Art 5 Cover

Art5 Pic1

The actual launching script for the Distraction Cannon is almost a straight copy of the firing mechanism for the normal rifle which you can see here, except with the ammo capacity set lower, the time between firing set higher, and sound effects switched out.

Art5 Pic2

So we'll move on to the script for the dummy itself, which is actually fairly simple.

Art5 Pic3

This block set “freezes” the dummy in place when it hits a surface so it seems like it sticks wherever it hits, which is actually just stopping all physical forces on the dummy and telling it to ignore gravity.

Edit: One of the reasons I'm doing these write ups is so I force myself to go back through my own thrown together code and find problems. After reviewing this one, I realized I'd made a hasty mistake and was actually setting the global gravity to 0 every time I fired a dummy. To fix this, I cut out the "Set Gravity" block and just set the rigid-body for the dummy to ignore gravity itself. As fun as it was to be able to moon bounce after firing the dummy, that was never really the intention. Back to the usual programming.

Art5 Pic4

This next block sets a scene boolean as true once the dummy spawns into the world to signal to the enemies that they should be turning their attention away from the player to the dummy. Then a block tells the script to pause for a few seconds so the player can do whatever they need to do, before setting the “Distraction” boolean as false so the enemies will turn their attention back to the player.

Art5 Pic5

Here’s a snippet of that enemy script in action.

Art5 Pic6

After that, there’s a block that spawns an explosion particle effect into the world at the position of the dummy, and a simple block for destroying the dummy when it’s no longer useful.

If you have any questions, feel free to reach out to me on Discord for more information or just to correct me on a stupid mistake, or join the Unity Visual Script Discord for some more highly skilled information.

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.