Home > Net >  OnTriggerEnter unity3d
OnTriggerEnter unity3d

Time:08-19

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?

CodePudding user response:

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
    }
}

CodePudding user response:

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

  • Related