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?
follow tutorials first
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.
I have emphasized the most important part. Could you please provide us with some code demonstrating how you manage this reset?
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.CompareTag("Zemin") && cc.IsTouchingLayers(LayerMask.GetMask("Zemin")))
{
zeminde = true;
ekstraZıpla = ekstraZıplama;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.CompareTag("Zemin") && cc.IsTouchingLayers(LayerMask.GetMask("Zemin")))
{
zeminde = false;
}
}
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(); " **
edit**: I think the problem is that I don’t know exactly what this code does.
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.
Id also recommend some codemonkey videos from youtube… he did a lot of 2d stuff for ages
Thanks everyone and thanks for the “code monkey” advice, I was looking at brackeys.
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 …