Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - Object in Unity won't trigger, although the colliders and code are correct

Since almost a week I'm testing Unity for making games. The problem is the following: I have an enemy and a circle around the player, which both has a collider with IsTrigger active. In the code, there is an OnTriggerEnter and OnTriggerExit event for the enemy. If the enemy enters the circle of the player, a bool gets true and other things should happen. But only this happens: The enemy goes right inside the player and only then the bool gets true (I tested it many times and this is it) even though the player itself doesn't have a collider, only the circle around it (the player has of course a character controller, but this can't be a trigger, can it?)

The enemy has a simple AI and always walks to the direction of the player so maybe it only triggers, when he is there. Because this it what seems to happen. The code is right here:

public GameObject thePlayer;
    public GameObject theEnemy;
    public float enemySpeed = 0.01f;
    public bool attackTrigger = false;
    public bool isAttacking = false;

    void Update () {
        transform.LookAt(thePlayer.transform);
        if (attackTrigger == false)
        {
            enemySpeed = 0.01f;
            theEnemy.GetComponent<Animation>().Play("walk");
            transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, enemySpeed);
        }
        if (attackTrigger == true && isAttacking == false)
        {
            enemySpeed = 0;
            theEnemy.GetComponent<Animation>().Play("attack");
            StartCoroutine(InflictDamage());
        }

    }

    void OnTriggerEnter()
    {
        attackTrigger = true;
    }

    void OnTriggerExit()
    {
        attackTrigger = false;
    }


    IEnumerator InflictDamage()
    {
        isAttacking = true;
        yield return new WaitForSeconds(1.1f);
        GlobalHealth.currentHealth -= 5;
        yield return new WaitForSeconds(0.2f);
        isAttacking = false;
    }

This Code actually belongs to Jimmy Vegas. I'm learning from his YouTube tutorial und in his video it worked fine. That's why I'm so confused.

Edit: The enemy ignores my player. Even when I add the collider to the player itself, it won't trigger. If I have the IsTrigger from the enemy turned off, he just glitches me away. I don't know what this is.

question from:https://stackoverflow.com/questions/65926688/object-in-unity-wont-trigger-although-the-colliders-and-code-are-correct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerStay.html

" Trigger events are only sent if one of the colliders also has a rigidbody attached "

Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...