Now for a very important bit if you're making a tank, firing.
Firing, as you might have guessed, is tied to the left mouse click, which according to Unity is Mouse 0. Right after the On Keyboard Input block, we're going to a Cooldown set to 0.5 seconds. So, the player can only fire a shell every 0.5 seconds at the most. In other games, I've implemented an ammo counter and I would also have a check after the cool down to see if ammo is greater than zero, but this is a nice simple tank game. Just a cooldown for this one.
The Get Variable block is here to orient the shell. The tank has an invisible little object attached to the barrel of the tank just beyond the box collider so the shell doesn't collide with the tank and explode right at spawn. This spot is the BulletPoint. We've attached the Variable Block to a Get Forward block so the shell knows which and then multiplying the forward force by 3 all across the board. So, Get Forward multiplied by a Vector 3 with all slots set to "3."
Now we're finally spawning our shell into the world with an Instantiate block. The Original slot is tied to a prefab of the tank shell. You can see a Get Position and Get Rotation block on the left side feeding into the Instantiate block. Those two are tied back to the Get Variable: BulletPoint block from the last slide. So, the shell spawns in at the Bullet Point and facing the right direction, which is away from the tank.
Then we finally move onto the Add Force block to get this shell moving. The little green line between the Instantiate and Add Force is hooked up the denote the force is being applied to the rigidbody of the newly created object. (Note: Your ammo will need a rigidbody if you want this to work. Play with the mass. It can be fun) The force Mode is set to Impulse because I wanted this to mimic a firing round and lose momentum after a bit, and the Force input comes from the Multiply block in the last slide.
So all the blue square was us figuring out what direction to push and how hard to push it, and then we dump all that information into the Add Force block to actually execute it.
Also I have this bit because I wanted the tank to make noise when it fires. The position is also set to the BulletPoint. And just below this is a zoom shot of everything so you can reference how it all hangs together.
That's it for now. Come back soon to learn more about how this tank works. Maybe throw me something on Ko-Fi and I can justify making some more Visual Script write ups, or buy a game. I have a lot of other half-baked scripts to share.