Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question I'm trying to learn how to code for a 2d game

Discussion in 'Getting Started' started by mahmutmr69, Feb 9, 2024.

  1. mahmutmr69

    mahmutmr69

    Joined:
    Feb 9, 2024
    Posts:
    4
    Hello, I tried working with Circle Collider and Collision 2D. When my character falls to the ground, my code does not work and my extra jump allowance is not reset. Whenever I delete Circle Collidier from my code, my code works as it should. I don't understand why, can you explain?
     
  2. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    329
    follow tutorials first
     
    MelvMay likes this.
  3. mahmutmr69

    mahmutmr69

    Joined:
    Feb 9, 2024
    Posts:
    4
    Thanks I've seen the tutorials on Unity. I usually try to find it by watching many videos on YouTube. However, I am having a hard time because I cannot find the exact problem specifically.
     
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    I have emphasized the most important part. Could you please provide us with some code demonstrating how you manage this reset?
     
  5. mahmutmr69

    mahmutmr69

    Joined:
    Feb 9, 2024
    Posts:
    4
    Code (CSharp):
    1.   private void OnCollisionEnter2D(Collision2D collision)
    2.   {
    3.    
    4.       if (collision.collider.CompareTag("Zemin") && cc.IsTouchingLayers(LayerMask.GetMask("Zemin")))
    5.       {
    6.           zeminde = true;
    7.           ekstraZıpla = ekstraZıplama;
    8.        }
    9.    
    10.   }
    11.  
    12.   private void OnCollisionExit2D(Collision2D collision)
    13.   {
    14.      if (collision.collider.CompareTag("Zemin") && cc.IsTouchingLayers(LayerMask.GetMask("Zemin")))
    15.       {
    16.           zeminde = false;
    17.       }
    18.  
    19.   }
    If I remove this " cc.IsTouchingLayers(LayerMask.GetMask("Ground")) " in my code, it worked as i want. The tag name of the ground my character touches is " Zemin " and there is a Circle Collider under my character for legs
    " cc = GetComponentInChildren<CircleCollider2D>(); " **
    edit**: I think the problem is that I don't know exactly what this code does.
     
    Last edited: Feb 11, 2024
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    If you're not sure how something works entirely, it's worth referring to the manual. Here we have a specific chapter on GetComponentInChildren.
    Surely one of the unwritten rules of using GetComponent is not using it at all. Why? Because there are more convenient ways to pass references to something than searching for them. Simply make the 'cc' field public or expose it to the editor using the [SerializeField] attribute. Then, in the script, you'll be able to drag and drop the desired component in the editor.

    Also, note that you need to meet two conditions to determine whether something is grounded. Touching the surface of water is different from immersing your entire hand in it. So, what you're probably interested in is the condition 'or' || not 'and' &&.

    I always recomend to start from Pathway, here is example of tutorial about same task you are doing now => Prevent player from double jumping

    Start of Junior Programmer Pathway.
     
    Last edited: Feb 11, 2024
  7. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,676
    Id also recommend some codemonkey videos from youtube.. he did a lot of 2d stuff for ages
     
  8. mahmutmr69

    mahmutmr69

    Joined:
    Feb 9, 2024
    Posts:
    4
    Thanks everyone and thanks for the "code monkey" advice, I was looking at brackeys.
     
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,676
    Brackeys was awesome, it was a shame he quit, but CodeMonkey is still at it, and in fact is apparently "soon" going to launch a complete interactive tutorial.. which sounds awesome ..