Search code examples
unity-game-engine2dcollider

Trigger Collinder doesnt work with Input.GetKey


I'm trying to make a game where u can cut down trees, but when I tested my script it didn't work at all. This script is for cutting down trees. When I put TakeDmg and Destroytree in the private void OnTriggerEnter2D(Collider2D other) it works fine, but when I add if(Input.GetKey(KeyCode.E)), it stops working I really don't know that's the problem.

public int treeHp;
public GameObject logPrefab;
public Animator anim;


private void OnTriggerEnter2D(Collider2D other){

    if(Input.GetKey(KeyCode.E)){

        TakeDmg();
        Destroytree();
    }
}
public void TakeDmg(){
    anim.SetTrigger("TreeDamage");    
    treeHp = treeHp -1;
}

public void Destroytree(){

    if(treeHp <= 0){
        //spawn la loguri 

        Destroy(gameObject);
    }
}

Thanks :D


Solution

  • You code would require the User to already hold down the E key before(or in the same frame as) hitting the tree.

    I would rather expect that you want to first move close to the tree and then press E in order to cut it.


    You should rather use OnTriggerStay2D

    Sent each frame where another object is within a trigger collider attached to this object (2D physics only).

    in order to listen for the E key as long as you are within the trigger. I would then also use Input.GetKeyDown in order to handle the press only once instead of every frame.

    Otherwise you would do

    anim.SetTrigger("TreeDamage");    
    treeHp = treeHp -1;
    

    ever frame while holing E down which is A) frame-rate-dependent and B) probabl not what you want to do here.

    public float cooldownDuration;
    private bool coolDown;
    
    // Listen for the key as long as you stay in the collider
    private void OnTriggerStay2D(Collider2D other)
    {
        if(!coolDown && Input.GetKeyDown(KeyCode.E))
        {
            TakeDmg();
            Destroytree();
    
            StartCoroutine(CoolDown());
        }
    }
    
    IEnumerator CoolDown()
    {
        coolDown = true;
    
        yield return new WaitForSeconds(cooldownDuration);
    
        coolDown = false;
    }
    

    As alternative if you actually want to continue cutting while holding down E you could do it like

    // Adjust in the Inspector: Seconds to wait before next cut
    public float cutInterval = 1f;
    private bool colliderExitted;
    
    // Listen for the key as long as you stay in the collider
    private void OnTriggerStay2D(Collider2D other)
    {
        if(Input.GetKeyDown(KeyCode.E))
        {
            StartCoroutine(CuttingRoutine())
        }
    }
    
    private void OnTriggerExit2D(Collider2D other)
    {
        colliderExitted = true;
    }
    
    private IEnumerator CuttingRoutine()
    {
        colliderExitted = false;
        while(!colliderExitted && Input.GetKey(KeyCode.E))
        {
            anim.SetTrigger("TreeDamage");    
            treeHp = treeHp -1;
    
            // Add a delay before the next iteration
            yield return new WaitForSeconds(cutInterval);
        }
    }