Saturday, December 28, 2024

Top Down Tank Tutorial: Firing

 

    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.

 

 

Monday, December 23, 2024

Top Down Tank Tutorial: Camera Controls

 

    Second bit of the tank tutorial, let's talk very quickly about how the camera is controlled. So, first of all, the camera isn't free floating or attached to the tank. It's instead attached to an empty object that follows the tank around, which let's me also detach it and move it around to see points on interest. 

    The empty object has this little bit of script that keeps it over the player when it isn't going after a point of interest. Every frame (On Update Block) the script grabs the position of the player (in this case called Player_Head) and sets its own position to match. Pretty simple.

    Then we have rotation controls. Anyone who read the last post will recognize these as copies of the rotation controls from the tank, but linked to the Q and E keys instead. Again, using rotation based on the self in the Rotation block. 

    I know this tutorial was a bit short, but the camera controls aren't all that complex, and do they really need to be?

     That's it for now. Come back soon to lean more about how this tank works. Maybe throw me something on Ko-Fi and I can justify making some more Visual Script write ups. I have a lot of other half-baked scripts to share.

 

 

Wednesday, December 18, 2024

Top-Down Tank Tutorial: Movement

 

    I made a simple little top down tank a while ago, and some parts were hard, some were easy. I thought I would share how I threw it together. So, first thing is first. We're going to talk about how the player gets the tank to move around the map.

 
    The controls for this tank are split into two main parts, basically turn and move. This chunk of script is built to rotate the body of the tank. We'll start with the top bit first. On Keyboard Input set to Hold, meaning the script will fire continuously while the A key is being held down. That's linked to a Cooldown block set to 0.02 seconds. I didn't want the tank to spin around too fast, and the Cooldown is there to regulate that. After that, we can see a Rotate block set to self and rotating -1 on the X axis, meaning the tank will turn left relative to itself.
    The second line on this chunk, coded to the D key with a positive 1 on the X axis, is obviously meant to turn the tank right.

    This bit moves the tank forward (relative to itself, not the world or camera). Again, we're using the On Keyboard with a Hold setting, linked to W for forward. Again, a Cooldown block so this isn't firing every frame, and then we are going to a Set Velocity block to move the tank forward. (Note that the tank needs to have a collider and a rigid body for this method to work.) 

    Down on the lower portion, we have a Get Forward block attached to This, so the script can figure out where the front of the tank is. That's fed into a Multiply block and pulling the tank speed from Object variables (which is set to 5 for this tank) and then fed to the Set Velocity block. So, the tank is given a continuous velocity of 5 in the "forward" direction as long as the player holds W.

    The Back portion of the script is virtually the same as the Forward chunk, except you can see the tank speed is multiplied by -1 before it is fed into the whole velocity thing. So, the tanks speed is set in the negative and the direction is reversed. Giving it a second look, I wish I had set the Cooldown slightly longer to make the tank reverse slower than it moves forward, but that's just a little personal preference. 

     That's it for now. Come back soon to lean more about how this tank works. Maybe throw me something on Ko-Fi and I can justify making some more Visual Script write ups. I have a lot of other half-baked scripts to share.


 

 

Tuesday, December 10, 2024

Micro Tutorial: Changing the Player Speed

 

    This is the script I used in Nuclear Lizard Island Rampage to slow down the player when they were stomping around in the ocean. It's relatively simple, one script to set the player to a fixed, lower speed when they get into the ocean (On Trigger Enter) and one to set their speed back to normal when they get out (On Trigger Exit). Both are hooked up to tag checks (which you can read more about here) to make sure the script doesn't trigger when some random things fall in the ocean, only the lizard. I should note that this script it attached to a trigger zone on the ocean itself, and not on the player character even though it controls the player speed. It was easier to do it this way than have checks on the players feet constantly looking out for water.

    You'll notice there are two different speed blocks, one for normal movement speed and one for sprint speed, that need to be set, and those are hooked up to a Scene Object variable through the green lines, which is linked to the player. You'll also notice on the bottom, we're resetting the players normal movement speed from a Saved variable. Since the player can upgrade their movement speed the reset speed need to be able to match their upgrade. We need to make sure we're setting them back to how fast they should be moving and not giving them a debuff when they get out of the water.

    Also on the Set Sprint Speed block, we're taking the players upgraded speed variable (Player_Speed) and running it through and addition block with a fixed variable of 3. I didn't want to mess with another up-gradable variable for sprint speed. So, I locked sprint at always 3 faster than walking speed. 

     That's it for now. Thanks for reading. If you want more maybe buy the game this script came from or just throw me something on Ko-Fi and I can justify making some more Visual Script write ups. I have a lot of other half-baked scripts to share.


 

 

Monday, December 9, 2024

Micro Tutorial: "Popping" UI

 

    This is a very small thing that I never thing of until I'm in the last rounds of polish with my game, but remember, pop your text whenever anything updates. I threw this script on virtually every piece of the UI in Nuclear Lizard Island Rampage after I brought the game to test and a convention and I kept getting comments like "You should really have [Any Text] as part of the UI" and then I would point out that specific thing as part of the UI that they just weren't looking at. So, change your text color, change the size, maybe have some audio along with it. Let's get into it.

    There are only three blocks in this and they're mostly self-explanatory. We pop the text size to 1.5 with a Set Local Scale and at the same time change it to a vibrant green with Text - Set Color, or if it's a UI element showing the player getting hurt, maybe choose red text. We then Wait for 0.4 seconds and then use the same blocks in reverse to set the text back to the neutral white and the local scale back to 1,1,1. Keep in mind, you will have whatever block starts this script set as a "coroutine" or the Wait block will break your game.

     Thanks for reading. If you want more maybe buy the game this script came from or just throw me something on Ko-Fi and I can justify making some more Visual Script write ups. I have a lot of other half-baked scripts to share.


 

Wednesday, December 4, 2024

Micro Tutorial: Comparing Tags on Trigger

 

    Yesterday(?) I wrote about how the tank turret in one of my games aims at the player, and a big part of getting that to work is letting the tank know that the player is even in range. This is done with triggers and tags.

    The tank itself is surrounded by a huge trigger sphere, but I don't want it firing off every time a crate is thrown through the trigger zone or a civi wanders through. So, after the On Trigger Enter block we go straight to a Compare Tag block hooked up (purple output/input) to an If block. So, if the object entering the trigger zone is tagged as the player, the script will fire and send the message "Liz_Seen" up to the parent object, which in this case it the tank, and that flips a boolean to let the tank know that the player is in range. 

    You can see in this picture there are two almost identical scripts, but note that one is starting with a Trigger Enter block and the other is a Trigger Exit. This way, the tank doesn't need to constantly check if the lizard/player is in range. It just gets notified when the player enters its range and when it exits the tanks range.

     Thanks for reading. If you want more maybe buy the game this script came from or just throw me something on Ko-Fi and I can justify making some more Visual Script write ups. I have a lot of other half-baked scripts to share.


 

Tuesday, December 3, 2024

Tutorial: Controlling a Tank Turret


 

    I like the phrase "Using a sledgehammer to crack walnuts" and this tutorial might just be that, but I'm going to take you through how I got the tank barrel to track the player in Nuclear Lizard Island Rampage, and maybe you can pull a better way out of this. The tank turret is actually made up of three separate independent pieces; the turret (round bit) the barrel (long bit) and the sight (invisible bit). The sight is the simplest and most important part. So, we'll go over that first. 

 


    The sight works off an incredibly simple billboard script. It's only job is to know where the players head is (Get Variable - Player_Head) and face it (Look At - Target) and it does it on an Update schedule, meaning every single frame. This script is slapped on an invisible object with no collider attached to the top of the tank. It's just there to act as a compass for the other bits.

    Side note, the "players head" is an empty object stashed inside the players capsule collider roughly where the head might be. I originally had this script just pointing at the player, but when I did that, everything pointed at the players feet since that was their origin/pivot point and it looked a bit weird.


    

     The tank turret (round bit) is also updating every frame. The first block it hits is an If block hooked up to a large, spherical trigger attached to the tank. If the enemy player is in the trigger area, a boolean gets flipped on and the If block reads as True, moving onto the block where we create a rotation. 

    Pulling from the Sight, which is hooked up as an object specific variable, we get the rotation and from that we pull out just the Y and W rotations. The X and Z have to stay at 0 so the turret only spins around on one axis. Then we move onto the Set Rotation block and feed in our very stripped down rotation data to the Turret, which is also an object specific variable. So, now we have the turret facing in the general direction of the player, but only when the player is in range, and we stopped it from looking up or down.



    The barrel (long bit) works in the exact same way as the tank turret, but we don't strip out any of the rotation data. So, the barrel is free to look the player directly in the eye (or camera) as long as they're in range. The whole effect only works because the turret and barrel are on the same update schedule and were modeled with the exact same pivot point, but it does work, and this whole mess of objects and scripts smoothly rotates to face the player whenever they get into firing distance.

     Thanks for reading. If you want more maybe buy the game this script came from or just throw me something on Ko-Fi and I can justify making some more Visual Script write ups. I have a lot of other half-baked scripts to share.