Yes, that’s how it works, I can re-write the code, it’s actually easier to make the player fall.
Yes but fall only if you are not “attached” if you look at the code I sent before you’ll understand better than I can explain lol
Like being able to jump freely if you are at the bottom and through it
I solved this with the previous code but didnt solve the falling and bouncing at the top
Also there were a bug where if you keept pushing space it would jump into space so i re-added the rb.velocity=vector2.zero before the jump and it worked good
So at least that revealed to be useful ahahah
There’s also an easier way to do this, but… well, not sure if I should introduced this to you yet, it’s a little advance, it’s called a State Machine, you can create one with the Mechanim system.
That would be cool but i would like first to solve this with the instruments i have to master them
Also i had another question, how did you attach the layer mask of the top? (I suppose it’s a game object and not a tile that ladder)
If I would like to do something similiar i should necessarily do an other entire tile layer where paint the “top ladders?”
Yeah, it’s a game object.
I have to understand how to do that in my situation (having also the ability to jump through the ladder etc…) i think that if I want to stick with this without using raycasts the only thing is to do a “TopLadders” tilemap to paint
So i think maybe it would be better to understand how to have the raycast and let him understand when I am at the top of the ladder colliders (as you said before) mhh
Don’t know if there are other solutions
I currently I have 1 capsule collider and 2 box collider on my player
The capsule is for world collisions and i made afer a little box collider at the bottom of the capsule near the feet to avoid the player from sliding when near edges, i really disliked that behavior from the capsule
If it was for me i would have used only a 1 box collider for the player but I read that for various reason it would be better to use a capsule so i did a sort of combination of the two
Then i have the collider Rick made in lesson at the bottom of the capsule for the Recognition of Ladders and the Ground( to jump) but i made it as a trigger because when it was not it’s collisions would mess up when jumping near edges and stuck the player
This is the reason why i have 3 colliders
Maybe i can substitute the collider i have for the ladders and ground with a boxcast (i think it’s called like this) to recognize top ladder colliders and ground
I think this could be a good strategy, what do you think?
That’s exactly what I’m doing.
Wow !
Ok, I kinda got this but is way too convoluted, so I suggest you try to manage this with animations. There are way too many bugs to handled them all in code.
Ok, I got an idea, but it’s gonna take me a little while, but I think I finally got rid of every single bug.
Ok thank you Yee, i’m still buzzled of how complicate is to get a perfect ladder ._.
This is gonna take a while, so keep trying.
I managed to do this without using a collider for the top of the ladder, I instead used an edge collider childed to the character, it has to be a little wider than the character for it to work correctly.
Here's the script
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Collider2D))]
public class Move : MonoBehaviour
{
[SerializeField] float speed = 4f;
[SerializeField] float climbSpeed = 2f;
[SerializeField] float jumpForce = 450f;
[SerializeField] LayerMask groundLayer = 0;
[SerializeField] LayerMask ladderLayer = 0;
[SerializeField] EdgeCollider2D ladderChecker = null;
bool isClimbing;
bool touchingLadder;
bool touchingGround;
bool onLadderTop;
float hAxis;
float vAxis;
Collider2D col;
Rigidbody2D rg2D;
private void Awake()
{
rg2D = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
}
void Update()
{
CheckCollisions();
CheckIfClimbing();
ReadMoveInput();
ReadJumpInput();
}
private void FixedUpdate()
{
rg2D.velocity = GetInputVelocity();
}
private void ReadMoveInput()
{
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = onLadderTop && Input.GetAxisRaw("Vertical") > 0 ? 0 : Input.GetAxisRaw("Vertical") ;
}
private void ReadJumpInput()
{
if ((touchingLadder || touchingGround) && Input.GetButtonDown("Jump"))
{
rg2D.velocity = Vector2.zero;
rg2D.AddForce(Vector2.up * jumpForce);
SetClimbing(false);
}
}
private void CheckCollisions()
{
touchingGround = col.IsTouchingLayers(groundLayer);
touchingLadder = col.IsTouchingLayers(ladderLayer);
onLadderTop = touchingLadder && !Physics2D.Linecast(ladderChecker.bounds.min, ladderChecker.bounds.max, ladderLayer);
}
private void CheckIfClimbing()
{
if ((!Mathf.Approximately(vAxis, 0) && touchingLadder) || onLadderTop) { SetClimbing(true); }
else if (!touchingLadder) { SetClimbing(false); }
}
private void SetClimbing(bool isClimbing)
{
this.isClimbing = isClimbing;
rg2D.gravityScale = isClimbing ? 0 : 1;
}
private Vector2 GetInputVelocity()
{
Vector2 newVelocity = Vector2.right * hAxis * speed;
newVelocity.y = isClimbing ? vAxis * climbSpeed : rg2D.velocity.y;
return newVelocity;
}
}
Great, i’ll look onto your code right now!
do you think i could use the edge collider also for other purposes? maybe making it a substitute for one of my existing colliders
it seems a little overkill to me to use 4 collider on a single character
this is my character right now
(little off-topic) (this is also an old pic)
i also i noticed zooming that i needed to put the collider a little bit up on the y-axis because there is some sort of 1 pixel ghost colliding, i read that this is a physics unity thing that you can modify but it’s advised to not to)
it detect the collisions with 1 pixel of distance
before checking your code i was trying to substitute the box collider which i use to check the ground to find if the player is on ground or on a ladder with an Edge collider, but it doesn’t work
isTouchingLayer doesn’t work with Edgecolliders?
it doesn’t seem to work even as a Physic collider , what am i missing(?)
i tested this also with a simple square and circle, the edge collider doesn’t do any collision to me .-.
maybe it is useful only for the Linecast thing and this sort of stuff (?) like am i only loosing my time? ahaahhah
ok i managed to get the Edge collider work by using the linecast, but as i said before now i have 4 colliders on my player, 2 to shape it’s physics collision in a optimal manner, and 2 for checking respectively the ground and the ladder(edgecollider) , i am trying to think a way to fully substitute the box collider for the jump check with the edge collider
what if i use a boxcast on the edge collider for the ground, and a linecast when near a ladder(?) don’t know if i am saying crazy things or not, my knowledge on raycasts are like 3%
i have an idea!
what if i delete the boxcollider i used for the ground check and the edge collider and i use the second box collider which was only used as an escamotage for the edges, and i cast a box from it when on ground for the jump check and a line which goes like from the center of the collider to up when on a ladder?
if this works i could do everything i need with only 2 colliders , with the box colliders used both for physics collisions and for the events