Search Unity

Unity 2D Tilemap Collider bug ?

Discussion in '2D' started by Shraneid, Oct 5, 2019.

  1. Shraneid

    Shraneid

    Joined:
    Oct 1, 2019
    Posts:
    2
    Hello everybody,
    I am kinda new to the unity editor for making games (used it mainly for machine learning up until now) and I don't understand how come what is happening is happening.

    It seems like there is a bug with the unity tilemap 2D collider or there is something I clearly don't understand
    It is a rather complicated thing to explain so I'll post a video so you can see the problem


    I am not changing the transform.position of my character via script nor am I touching to colliders.
    Also, I've been having a lot of clipping through textures and popping back to the top, is it normal / should I use something else or did I mess up ?

    Thanks in advance for your help !

    Here is the script for my player
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     public float speed = 0.12f;
    4.     public float y;
    5.     public float z;
    6.  
    7.     public float jump_force;
    8.     public bool jumping;
    9.  
    10.     Rigidbody2D rb;
    11.  
    12.     void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.         jumping = false;
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         transform.Translate(Input.GetAxis("Horizontal") * speed, y, z);
    21.  
    22.         //CANT GET LEFT OF THE MAP
    23.         /*if (transform.position.x < -11)
    24.         {
    25.             transform.position = new Vector3(-11, transform.position.y, transform.position.z);
    26.         }*/
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         if (Input.GetAxis("Horizontal") < 0)
    32.         {
    33.             transform.localScale = new Vector3(-1,1,1);
    34.         } else if (Input.GetAxis("Horizontal") > 0)
    35.         {
    36.             transform.localScale = new Vector3(1, 1, 1);
    37.         }
    38.  
    39.         if (Input.GetKeyDown(KeyCode.UpArrow) && !jumping)
    40.         {
    41.             rb.AddForce(transform.up * jump_force, ForceMode2D.Impulse);
    42.             jumping = true;
    43.         }
    44.     }
    45.  
    46.     private void OnCollisionEnter2D(Collision2D collision)
    47.     {
    48.         if (collision.gameObject.CompareTag("Ground"))
    49.         {
    50.             jumping = false;
    51.         }
    52.     }
    53. }
     
  2. coidevoid

    coidevoid

    Joined:
    Oct 26, 2017
    Posts:
    46
    You probably want to use velocity instead
     
  3. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,602
    transform.Translate(Input.GetAxis("Horizontal") * speed, y, z);


    This is directly changing transform.position. Don't use the transform for movement, the rigidbody controls the transform on its own, so use it for everything you want to work properly with physics.

    If you want instantaneous movement, use "Rigidbody.MovePosition"

    Can't say if this is causing your issue specifically, but it's definitely going to throw a wrench into the mix.
     
  4. Shraneid

    Shraneid

    Joined:
    Oct 1, 2019
    Posts:
    2
    Thanks a lot for answers !
    I'll try this soon