Inconsistent Jump height

so i managed to get the BoxCast, but i am struggling with the raycast, why raycast? because i’m trying as i said to use only 2 colliders and the edgecolliders worked well but i didn’t understand how to get the line to be instantiated with an offset (without using the edge collider), so i created a raycast but i don’t understand why it works like this:

it’s behavior is different from boxcast/linecast while they detect collision while the player is moving inside the collider, the raycast to me act more like a “penetrating” arrow, like it detects the collision while it’s end penetrates the collider and same when it exits, i want it to behave like a boxcast/linecast


like this it detects (the red is the box collider of the ladder)

like this no, so only while “penetrating”

don’t know if this is expected, i’m learning raycasts etc… thanks to you with bruteforce approach lol

and i know i’m talking alone since 4 messages but this helps me to clear my mind hahaaha

PS: i just realized that maybe all collisions work like this ._.

1 Like

Sorry, I can be here for just a couple of hours each day, otherwise I wouldn’t be able to work on my projects.

Don’t be afraid of using multiple colliders, that’s what Unity is for, I see a lot of beginners trying to code everything, in reality, you should try and avoid using code, if there’s a solution that allows you to simplify or even not use code at all, use that. The edge collider also gives you a visual representation in real time, meaning you can tweak it far more easily than using code, it has way more benefits than coding everything, of course, you could create your very own custom editor/inspector, but that’s overkill when the tools Unity provides can do the same thing.

The behavior you are describing is not something I have noticed before, I have to experiment because I don’t think that’s how that works, maybe your math is off.

1 Like

I tested this and I noticed that they work exactly the same way, none of them worked as a penetrating arrow, they both detected the collider while inside it, maybe your math is off.

CheckingRayAndLine

This is the code I used, the math you need to use Raycast and Linecast is different, the first asks for a direction, the second asks for an end.

[SerializeField] float rayDistance = 0.5f;
[SerializeField] LayerMask maskToHit = 0;

void FixedUpdate()
{
    RaycastHit2D hit2D = Physics2D.Raycast(transform.position, Vector2.up, rayDistance, maskToHit);

    if (hit2D.collider != null) { Debug.Log("Raycast is hitting something"); }

    RaycastHit2D linecastHit2D = Physics2D.Linecast(transform.position, transform.position + Vector3.up * rayDistance, maskToHit);

    if (linecastHit2D.collider != null) { Debug.Log("Linecast is hitting something"); }
}

private void OnDrawGizmos()
{
    Gizmos.DrawLine(transform.position, transform.position + Vector3.up * rayDistance);
}
1 Like

Hi, yee don’t worry
This is really weird, my boxcast only works if it is wider than the ladder collider, otherwise if it is only “contained” in it it doesn’t detect the collisions

I was reluctant to use many collider on one player because I read it was performance heavy .-.

1 Like

It is performance heavy but you are making a 2D game, you shouldn’t worry too much about that, and it’s also worth noting that is more performance heavy to have to recalculate the positions of the boxcast each frame instead of having a somewhat static collider.

1 Like

But aren’t we using the edge collider to cast the line from it’s position? Like this we are using both lol
Btw i almost did it but I am stuck in a limbo where i got all the other functions but now when I press up he goes all the way up the ladder and not controlled by me, like an elevator, unfortunately your code was fabulous but a little hard to understand cause i am used to if statements etc and i tried to adapt it to my code so i think i made some mistakes, from what you said i think i’ll delete the box cast and simply use 4 colliders
2 for the physic shape of the character
The box for the detection of ladders and ground, and the edge for cadting the line for the detection of the top, maybe i can somewhat use the box to attivate the component of the edge only when i am near a ladder, if i can do that(?)

ok i managed to fix that and i cleaned up my code but now instead i cannot jump from the ladder… hahahahahaah from the base and through yes, top and while climbing no :thinking:

UPDATE: i fixed this but now when i jump from the ladder i do a uber big jump and also i noticed that i cannot land on the top of the ladder if i jump on it :thinking: :thinking: (like from an upper place to the top of the ladder)

UPDATE2: I noticed that the landing problem is due to the colliders , distanciating them makes the collision work better but i also noticed that sometimes it’s like the collision it’s identified earlier and sometimes “later”, i’ll explain myself better with a brief video

as you know the top landing we did works by identifying when both the edge collider does not collide with the ladder and the ground box collider detects the ladder, if i make them more distanciated the collision work “better” but the top is identified with an offset and it stops the character in a lower place, why this happens, the collisions are working in a weird way

Here’s the video, also as you can see when i drop the player from an higher place it becomes more difficult for it to detect the collision correctly

i forgot to let you see here that now it works all except for this collision thing and the fact that if i jump from the ladder the jump speed gets like a 2x boost but i think this is a stupid bug i can get my self into solve

This is how my part of the ladder code looks right now



    private void Update()
    {
        CheckInput();    
        Jump();
    }

 
    private void FixedUpdate()
    {
        CheckIfClimbing();
        CheckCollisions();
        Move();       
    }

    private void CheckInput()
    {
        dir = Input.GetAxisRaw("Horizontal");
        if (onLadderTop && Input.GetAxisRaw("Vertical") > 0)
        {
            vAxis = 0;
        }
        else
        {
            vAxis = Input.GetAxisRaw("Vertical"); 
        }  

       
        checkjump = Input.GetButtonDown("Jump");
    }


    private void CheckCollisions()
    {
        onGround = myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground"));
        onLadder = myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"));
        onLadderTop = onLadder && !Physics2D.Linecast(ladderChecker.bounds.min, ladderChecker.bounds.max, LayerMask.GetMask("Climbing"));
  
    }

    private void Move()
    {

         Vector2 PlayerVelocity = new Vector2(dir * moveSpeed, rb.velocity.y); //calcolo velocita x

        if (isClimbing)
        {
            PlayerVelocity.y = vAxis * climbingSpeed;
        }
        else
        {
            PlayerVelocity.y = rb.velocity.y;
        }
      
         rb.velocity = PlayerVelocity;            

    }



     private void CheckIfClimbing()
    {
        if((!Mathf.Approximately(vAxis, 0) && onLadder) || onLadderTop) 
        {

            isClimbing = true;
            rb.gravityScale = 0; 
            
        } else if (!onLadder)
        {

            isClimbing = false;
            rb.gravityScale = gravityScaleAtStart;

        }
     
    }


    private void Jump() 

    {
     
       if ((onGround || onLadder || isClimbing) && checkjump)

        {
            isClimbing = false;
            Debug.Log("salto");           
            rb.velocity = Vector2.zero;        
            Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
            rb.velocity += jumpVelocityToAdd;
            return;


        }


    }



Yes, but actually no. It’s a little weird, let me explain.

Imagine you want to cast a line from the player’s position to another point. If you hard code that Unity will use far more resources than if you use a transform, child it to the character and use it as a reference to cast the line.

Why does, using a collider or transform as a reference, which is using more objects and almost the exact amount of code, work better? To be honest, I don’t know, but that’s how Unity works, I suppose it has something to do with the prefabs and child systems, since those are fully integrated into the engine they are super-efficient.

To better detect your collisions you’ll have to detect them inside a FixedUpdate, in my code I didn’t do it because it causes a lot of other issues with the input, and you can’t put the input inside a FixedUpdate, it won’t work like 1% of the time, which sounds ok-ish, but since it runs I don’t know how many times per frame that 1% is way too much. You could clamp the Y velocity so you don’t have to make a massive refactor.

What you could also do, which involves almost starting from scratch unless you are good enough with refactoring, is create a script that handles collisions, it will give you more breathing room, and actually, that’s how it’s done more often than not, handling collisions, movement, and input in the same script is considered bad practice, but it’s a great way to start learning how to code, having to jump to 3 different scripts can be scary for beginners.

1 Like

Does this work better even if i didn’t put the collider on children(?) because until now i had the paradigma given by Rick to put on children only like sprite and rendering stuff and leaving the logic onto the parent

really on other parts of the course we did a lot of scripts and we used onCollision OnTriggers etc… but here i am just following what Rick primarly did, maybe it’s because this part of the course was made earlier that the ones before

do you think that maybe a onTriggerEnter etc… and maybe then checking for the layers would work better? tbh i am pretty tired of this ladder , it’s been almost 3 days im stuck with this pieces of wood, i would like to end this

also if you give a look onto my code you can see i already check for collisions in fixed update, yet they are still detected like shit and when the player is dropped from really above they get messy :sob:

also i remember you i am using tilemaps, don’t know if it is an useful info

Not sure if this has anything to do with the Rigibody then, check its Collision Detection, see if it is set to Continuos, that might help, but I’m not completely sure.

OnTriggerEnter Would definitely work, you could try that too.

Another suggestion, but you’ll probably get stuck with this a little longer, is to look into something called “State Machines”, which is a coding pattern that is exactly what you are trying to do here, the result I got from using that was far better than anything we tried, unfortunately, it might be a little advance, but hey, if you manage to get it right you can totally start using it not just to create fancy behaviours for your character, but also for the AI.

as i said before the first thing i’ll look after this will be state machines trust me, but before i’d like to solve this with these instruments ahahh
it’s already set to Continuos, I also don’t think it’s related to fixed update or update, if you loook at my code you’ll se i already put all the physics related stuff in Fixed and the input etc in Update, also after we do the checkinput in the Update later when i use them in other functions it’s only like i am reading the bool value i stored when i checked it in Update, so i think this is not the problem, (i already tried too)
don’t know why the Jump get boosted why on the ladder and why the collisions are so messed up

(also i remember again to you i am using tilemaps for the ladder, don’t know if it is an useful info, which have a COMPOSITE collider attached to)

After some tests… yeah, collisions are not going to work correctly with that sort of code and using OnTriggerStay will throw the same results according to Unity documentation, you’ll need to find out another way, the best idea I have right now is to use a component called Platform Effector 2D, the issue is that you won’t be able to get down when at the top of a ladder, so you’ll need to find a way to solve that.

My final suggestion would be to study how others games do it, Megaman X has a lot of stairs and they work perfectly, same goes for Mario games, you might want to look into them.

1 Like

i solved the jump issue, it seems like not setting the gravity scale in the jump method caused an issue where it went from 0 to 7 like a litttle bit later and it caused like a sprint (?) anyway i solved everything,
right now the only “issue” is this collision thing even if unlike the other ones i can let it a little bit more as a “working progress” or like a “ok i’ll be happy as it is” because i’m a little tired and it’s not like it doesn’t really work , it’s just a little unprecise mh :sob: :sob:

wherever i looked through the web everyone suggest what we did, i didn’t think a good ladder would have been so difficult to make

the only thing i can think of is to make some sort of invisible layer , with invisible cubes with box colliders which would activate only when i’m up the ladder/ not on it/not pressing down/ so that when jumping i would collide to it and then deactivate it. now that i think on this it could be a really good idea
it seems like this guy did something similiar
Ladder Climb: Turning the Mega Challenge into an Uber Challenge - Unity Courses / Show - GameDev.tv

the only problem would be that i should manually put everytime this on top of an existing ladder :confused:

Look into the component Platform Effector 2D, you are probably even going to use it during this section of the course, it will solve the issue of standing on top of the ladder and the character can climb through it, you’ll only need to find a way to get down when at top of the ladder.

1 Like

Thank you, i think i’ll improve my ladder later when i will have more knowledge, i have been stuck for too long here, it’s time to go on, still it was time worth spending, cause i learnt a lot of stuff, just tired cause it took too much time to get reasonable results

1 Like

Welcome to Game Development! :sweat_smile: :sweat_smile: :sweat_smile:

Privacy & Terms