Combat help for my first game?

Hi there!

Now, I’ve been following a tutorial on Youtube by the “Lets make a game together” guy, and I’ve been pretty much doing the exact stuff he is up until tutorial 6(which is where I stopped and right now I’m deciding how I want my game made).
I’m making my first game right now and it’s a 2D platformer. I’ve fiddled with graphics, movement, animation, and I kinda understand all that, but the problem I have is combat(writing the script for it).

The way I see it is I want my character to be able to “kill” enemies with a press of a certain button(I did find the instructions on how to bind a certain key, but that script just messed up everything last time in my previous code, and I had to reroll a couple of episodes back :frowning: ).
I made the animation for attacking, although the hitbox is not extending/moving, it’s staying in the same place, it’s just my “polar bear” protagonist just swings his paw to animate the attack(it’s so simple and not-so-polished, it’s my first time, so I’m just trynna get some basics).

The way I wanted to do it is to add a script on my player character that will detect if a certain button has been pressed, and if it was, it will start an extremely short count-down timer during which it will detect the hitbox collision and if the hitbox of a collided object has a tag “enemy” it will destroy it. That’s the way I did it previously, and as I mentioned, it screwed with the code, so I had to go back and re-do everything(which really disappointed me, I thought I had made something of my own). Also, I would like to program my enemy(them penguins) to throw snowballs at my bear every time they face the same direction the bear is at(I made the graphics, animation, the implementation of the idea is a problem). I also wanted their spawn to be kinda scripted, at certain “spawn-points” I could place in the game, but I figured it would be too complicated for a beginner like me and I will probably hand-place them all myself.

It would be nice if I could have some coding help, as it’s my first time doing some real coding(I did learn in school, but no problem solving was ever done whatsoever).
P.S. if you want me to include any files, just say it and I’ll do it.

"The way I wanted to do it is to add a script on my player character that will detect if a certain button has been pressed, and if it was, it will start an extremely short count-down timer "

so you have a couple of options

  1. detect when a certain key was pressed
  2. detect when a certain bindable key was pressed

The difference is the second option allows the player to rebind keys (so you might have set “attack” when they press the F button, but they change it to K instead.

Method 1 (Hardcoded)

Method 2 (allow the user to rebind key)

  1. Open InputManager (edit > project settings > input)

  2. create a new input called KillEnemy and set the positive key as whatever you want (lowercase) b, c, k, a, f etc.

    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour {

     public int CountdownStart = 60;
     public bool countdownStarted = false;
    
     void Update() {
         // alternate version for hardcoded key
         // if (Input.GetKeyDown(KeyCode.F)
         if (Input.GetButtonDown("KillEnemy") && countdownStarted == false)
         {
             print("Kill Enemy button was pressed");
             InvokeRepeating("StartCountdown", 1.0f, 1.0f);
             CountdownStarted = true;
         }
     
         EndGame();
         
     }
     
     // remove 1 from CountdownStart every second
     void StartCountdown()
     {
         CountdownStart -= 1;
     }
    
     // do something when countdown reaches 0
     private void EndGame()
     { 
         if (CountdownStart == 0)
         {
             // do something when timer reaches 0
         }
     }
    

    }