Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Detect collision with terrain

Discussion in 'Scripting' started by MikeyJY, Mar 4, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I know there are some forums about this but I have other colliders:
    Terrain(look in inspector):
    upload_2020-3-4_17-18-5.png


    and the object(Box trigger collider):
    upload_2020-3-4_17-18-38.png

    I tried this in a script attached to the object:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other){
    2.    if(other.GetComponent<Terrain>() != null){
    3.       collide = true;
    4.    }
    5. }
    It is not working.

    i also tried this:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other){
    2.    if(other is TerrainCollider){
    3.       collide = true;
    4.    }
    5. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    See the "Collision action matrix" at the bottom of this manual page.
    https://docs.unity3d.com/Manual/CollidersOverview.html

    A static trigger collider will not trigger another static trigger collider, which appears to be the case here from your screen shots. It also looks like you're using them in a way specifically not recommended. From the same manual page, emphasis mine:

     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    But Why do you say that I have 2 triggers collider, because only the object has trigger. the terrain has a terrain collider and I think that the TerrainCollider isn't a trigger!
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I think you should just remove the mesh collider on the terrain.
    I'm pretty sure for a trigger to happen only one object needs to be set to a trigger, if both are triggers it's like they're invisible to each other, don't use triggers that much though -- so not sure..

    @Joe-Censored - it's no longer the case since Unity 5 I believe, someone at the Unity team just refuses to update those docs (along with a few other really bad ones).
    I think I saw @yant and a few others say so (not about somebody outright refusing to change the docs).
     
    Joe-Censored likes this.
  5. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Thank you, but I tried to remove the mesh from terrain but I isn't detect the collision. Anyway, I found another way to do what I wanted without collision with the t1errain, but probably, in future, I will need the same thing for other projects, so I'm still searching for this.

    (I forgot to mention that the moving of the object is an animation) I don't know if that is the reason because the collision isn't detected.
     
  6. kobyfr

    kobyfr

    Joined:
    Aug 21, 2020
    Posts:
    7
    seems you are right, and that its impossible for a trigger to fire when an object touches or passed through a terrain, without adding something extra, besides the terrain collider on the terrain side, or in addition to the IsTrigger collider on the object side.
    MikeyJY, how did you accomplish this, eventually?
     
  7. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I don't really remember sorry. I think I didn't use the trigger check at all.
     
    kobyfr likes this.