Search Unity

Retrieve contact point on collision with tilemap

Discussion in '2D' started by darksteph, Oct 26, 2020.

  1. darksteph

    darksteph

    Joined:
    Dec 13, 2019
    Posts:
    5
    Hello,
    I'm making a little 2D game and i'm looking for contact point on tilemap.

    First gameobjet :
    - CircleCollider2D -> isTrigger = false
    - RigidBody2D -> Dynamic with mass = 0

    Second GameObject
    - Tilemap renderer
    - TilemapCollider2D -> isTrigger = false
    - RiggidBody2D -> Dynamic with mass = 0
    - Script for detecting collision
    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         Debug.Log("contact");
    4.         if (collision.gameObject.tag == "IceAura")
    5.         {
    6.             Debug.Log("Contact with Spell");
    7.             ContactPoint2D contact = collision.GetContact(0);
    8.             Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
    9.             Vector3 pos = contact.point;
    10.             Debug.Log(pos);
    11.         }
    12.         else if (collision.gameObject.tag == "joueur")
    13.         {
    14.             tm.isTrigger = true;
    15.         }
    16.     }
    My probleme is the OnCollisionEnter2D not calling when the two gameobjects (Spell of player and water) collided.
    I tried all configurations (isTriger true/false, with Rigidbody or not etc.)
    I can't use OnTriggerEnter2D (this function is ok) because i want the exact collidePoint between the two objects.

    Thanks for your help.

    Stéphane
     

    Attached Files:

  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share the settings for your GameObjects with screenshots of the Collider2Ds and other Physics2D Components for the Spell of player and water GameObjects?
     
    darksteph likes this.
  3. darksteph

    darksteph

    Joined:
    Dec 13, 2019
    Posts:
    5
  4. darksteph

    darksteph

    Joined:
    Dec 13, 2019
    Posts:
    5
    @ChuanXin : below informations.

    Ice is the moving gameobject.
    Tilemap is fixed gameobject. The script that detects the collision is in Tilemap.

    Sorry but i don't found a solution to get properties with json.

    Thanks for your help.
     

    Attached Files:

    Last edited: Oct 29, 2020
  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Just to check, in Ice_1 and Ice2.png, it seems that the Circle Collider 2D is disabled? The checkbox next to the name of the component is not checked. This means that there will be no collisions for this GameObject.
     
  6. darksteph

    darksteph

    Joined:
    Dec 13, 2019
    Posts:
    5
    ChuanXin,

    thanks for your help.

    Circle collider 2D is enabled by Animator.
    See video screen on link https://drive.google.com/file/d/1X16phyCXoEtOY4fLr73x9y2ln3NX6E-S/view?usp=sharing

    Best regards,
     
  7. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    upload_2020-11-3_12-30-46.png

    0.08s

    The alert in the RigidBody2D suggests that the enabled Circle Collider 2D is not in the simulation which could be causing the issue. I'll ask about this!
     

    Attached Files:

  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,450
    Totally correct yes. If you take it out of the simulation then it's not going to be part of it so all attached colliders, joints, effectors and associated contacts/callbacks won't happen. Doing this is the most efficient way of doing that but you can't take it out of the simulation and somehow expect it to be in the simulation.
     
  9. darksteph

    darksteph

    Joined:
    Dec 13, 2019
    Posts:
    5
  10. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    It looks like the Ice Aura Circle Collider 2D has "Is Trigger" set. That would correspond with the OnTriggerEnter2D method instead of the OnCollisionEnter2D. Could you check that if "Is Trigger" is unset, your code is called? Thanks!
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,450
    Additional to what was said above, for your tilemap/composite you've got all the constraints set to stop it moving and you've set its mass to 0.0001. That's what a Static body-type is for so change the body-type property to Static. It's not clear why you've set all your other masses to be so very small too; 0.1 of a gram!
     
    qbvbsite likes this.