Home:ALL Converter>How do I use OnTriggerEnter() on a non-moving object?

How do I use OnTriggerEnter() on a non-moving object?

Ask Time:2022-10-15T01:17:15         Author:Business Cactus

Json Formatter

I'm a new to game developing, and I'm making my first game in Unity, which is a top-down, 2D survival type game. In order to detect when the player hits a tree or other world object, I added invisible triggers on each side of the player, which I set active whenever you click. Whenever either the player or the target is moving, this system works perfectly, however, when the target is not moving, like a tree, the collision is not detected. I figure that the OnTriggerEnter function only works when a moving object collides with the trigger, however, I have no idea how to do it otherwise. Is there another function I can use, or some way I can fix this?

void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("hit");

        if (other.gameObject.tag == "Tree")
        {
            Debug.Log("hit tree");
            other.gameObject.GetComponent<TreeScript>().treeHealth--;
        }
    }

Author:Business Cactus,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74072673/how-do-i-use-ontriggerenter-on-a-non-moving-object
yy