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

Question Tilemap composite collider does not stop player

Discussion in '2D' started by koselerlie, Aug 23, 2023.

  1. koselerlie

    koselerlie

    Joined:
    Feb 17, 2022
    Posts:
    5
    Hi,
    My main character swing around and sometimes it can stuck inside colliders.
    I am using tilemap colliders with composite collider. When I use box collider , everything seems to ok but It make everything less detailed and hard. Can anyone know what to do? Can I add something else or should I put speed barrier to my char?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Since you posted absolutely zero information, it's hard to say.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
    koselerlie likes this.
  3. koselerlie

    koselerlie

    Joined:
    Feb 17, 2022
    Posts:
    5
    To make it clear. I have took some video.
    Here is the link: 23.08.2023_17.22.17_REC (screenrec.com)

    Also I want to give you my code that I have use for grapling effect.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.Experimental.GraphView;
    4. using UnityEngine;
    5.  
    6. public class SpecialMove : MonoBehaviour
    7. {
    8.     public leftMovement left;
    9.     public Movement right;
    10.     public SpringJoint2D leftSpring;
    11.     public SpringJoint2D rightSpring;
    12.     public Rigidbody2D rb;
    13.  
    14.     private GameObject node;
    15.     public int forceAmountX = 1;
    16.     public int forceAmountY = 1;
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.     // Start is called before the first frame update
    24.     private void Update()
    25.     {
    26.      
    27.      
    28.     }
    29.  
    30.     private void FixedUpdate()
    31.     {
    32.         if (leftSpring.connectedBody == null && rightSpring.connectedBody != null  || leftSpring.connectedBody != null && rightSpring.connectedBody == null)
    33.         {
    34.             if (leftSpring.connectedBody != null)
    35.             {
    36.              
    37.                 node = leftSpring.connectedBody.gameObject;
    38.             }
    39.             else if (rightSpring.connectedBody != null)
    40.             {
    41.              
    42.                 node = rightSpring.connectedBody.gameObject;
    43.             }
    44.  
    45.  
    46.             if (transform.position.x < node.transform.position.x - 1)
    47.             {
    48.                 if (Mathf.Abs(rb.velocity.x) < 40)
    49.                 {
    50.                     rb.AddForce(Vector2.right * forceAmountX * Time.fixedDeltaTime, ForceMode2D.Force);
    51.                     rb.AddForce(Vector2.down * forceAmountX * Time.fixedDeltaTime, ForceMode2D.Force);
    52.                     forceAmountX += 1;
    53.  
    54.  
    55.                 }
    56.             }
    57.             else if (transform.position.x > node.transform.position.x + 1)
    58.             {
    59.                 if (Mathf.Abs(rb.velocity.x) < 20)
    60.                 {
    61.                     rb.AddForce(Vector2.left * forceAmountX * Time.fixedDeltaTime, ForceMode2D.Force);
    62.                     rb.AddForce(Vector2.up * forceAmountX * Time.fixedDeltaTime, ForceMode2D.Force);
    63.                     forceAmountX += 1;
    64.  
    65.  
    66.                 }
    67.             }
    68.             else
    69.             {
    70.                 forceAmountX = 1;
    71.             }
    72.  
    73.             if (rb.velocity.y > 20)
    74.             {
    75.                 rb.AddForce(Vector2.down * forceAmountY * Time.fixedDeltaTime, ForceMode2D.Force);
    76.                 forceAmountY += 1;
    77.             }
    78.             else if (rb.velocity.y < -20)
    79.             {
    80.                 rb.AddForce(Vector2.up * forceAmountY * Time.fixedDeltaTime, ForceMode2D.Force);
    81.                 forceAmountY += 1;
    82.             }
    83.             else
    84.             {
    85.                 forceAmountY = 1;
    86.             }
    87.  
    88.  
    89.         }
    90. //This part is where I do grapling
    91.         else if (GameObject.ReferenceEquals(leftSpring.connectedBody.gameObject, rightSpring.connectedBody.gameObject))
    92.         {
    93.          
    94.                 Vector3 direction = leftSpring.connectedBody.gameObject.transform.position- transform.position ;
    95.                 Vector2 realDir = new Vector2 (direction.x, direction.y);
    96.              
    97.                 leftSpring.enabled = false;
    98.                 rightSpring.enabled = false;
    99.  
    100.             StartCoroutine(addForce(realDir));
    101.             }
    102.         else
    103.         {
    104.             node = null;
    105.          
    106.         }
    107.  
    108.      
    109.  
    110.     }
    111.  
    112.     IEnumerator addForce(Vector2 direction)
    113.     {
    114.         rb.AddForce(direction * 3 * Time.fixedDeltaTime, ForceMode2D.Impulse);
    115.         yield return new WaitForSeconds(10);
    116.     }
    117.  
    118.  
    119. }
    120.  
    Other than this class there is no class that I apply force to my character.

    Thank you for your feedback
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    You don't show what the CompositeCollider2D geometry mode is i.e. have you asked it for polygons or outlines (edges)? If edges then are you aware of the difference between Discrete and Continous collision detection on a Rigidbody2D? Discrete means it can step over thin colliders or thicker colliders if it's moving fast but this mode is fast to calculate Continuous stops this collision tunnelling by calculating the impact point no matter how fast but it's more expensive to calculate.

    So, if you are asking for composite edges and you're using Discrete collision detection mode on the Rigidbody2D then you're likely just tunnelling so use Continuous for that Rigidbody2D.
     
    koselerlie likes this.
  5. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    302
    Tip: Do NOT use polygon collider mode if you want to avoid ghost collisions.

    Also, make sure to set Offset Distance property on the Composite Collider 2D to 0 or you could still get ghost collisions.
     
  6. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    There are a few other problems with your code, they are probably not causing your issue with collision, but would be good to fix anyway.

    Code (CSharp):
    1.  rb.AddForce(Vector2.right * forceAmountX *  Time.fixedDeltaTime, ForceMode2D.Force);
    You can't multiply force by time like that it's wrong. Multiplying stuff by deltaTime isn't magic solution for making things frame independent that you can sprinkle everywhere. Doing so in the wrong place without understanding physics can equally likely have the opposite effect and make the code more frame dependent.

    This coroutine is nearly useless you are immediately adding impulses and afterwards waiting 10 seconds to do nothing. Might as well add the impulse directly without coroutine. Waiting within coroutine only delays code following within the coroutine, it has no effect outside the coroutine. Also again nonsensical multiplication by deltaTime.
    Code (CSharp):
    1.  
    2. IEnumerator addForce(Vector2 direction)
    3. {
    4.   rb.AddForce(direction * 3 * Time.fixedDeltaTime, ForceMode2D.Impulse);
    5.   yield return new WaitForSeconds(10);
    6. }
    7.