简体   繁体   中英

OnTriggerEnter unity3d

Please tell me, there is a function OnTriggerEnter:

void OnTriggerEnter(Collider other) {}

This function matches if an element is in the trigger of another element.

Now this script is on the character.

How can I perform this function for my character by hanging a script for example on terrain?

There is a lot of left out context in this question, and I hate to make assumptions, but I can't yet comment so I will try my best to answer. Assuming that you want to detect if the character is colliding with the terrain, the easiest way to do this, would be to instead use void OnCollisionEnter () . If your player character has a Rigidbody and a collider component, and your terrain has a collider component, you can tag your terrain "Terrain" and detect for collisions with it using the following function in your code:

//Detects a collision and grabs the collider
void OnCollisionEnter (Collider other)
{
    //Detects if the object collided with has the "Terrain" tag
    if (other.gameObject.tag == "Terrain")
    {
        //Your code here
    }
}

Looks like you want to know how to use OnTriggerEnter Here are the steps.

  1. Attach Colliders to the objects that are expected to collider. At least one of the Collider should be marked as trigger.
  2. Attach a non-Kinematic Rigidbody to one of the objects.
  3. attach the script with OnTriggerEnter function to one of the objects. The function will be called if the collision is detected.

You can read this tutorial on Unity Collision for detailed explanation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM