Doodle Jump Platform Script

Hey guys!

I’m trying to program a game similar to Doodle Jump. Everything works except that a certain platform is not working properly. I can jump on it but I can’t jump through below, I always have to jump around the platform. Can you help me?

here is my script:

public class Springer : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

private void OnCollisionEnter2D(Collision2D collision)
{

if(collision.gameObject.GetComponent().velocity.y <= 0)
{

collision.gameObject.GetComponent().AddForce(Vector3.up * 1000f);

}else
{
if (collision.gameObject.GetComponent().velocity.y > 0)

{

collision.gameObject.GetComponent().velocity = new Vector2(0.0f, 10f);

Debug.Log(“colisione xD”);

}
}

}
}

Try putting an empty gameObject as a child of your player, and move its transform so it is right at the bottom of the player’s sprite (at its feet). Then try a script similar to this on your platform (Im assuming it has a box collider):

public Transform PlayerBottom; //reference to the empty object you just placed at the Player's feet
BoxColider2D box;

void Start(){
      box=GetComponent<BoxCollider2D>();
}
void Update(){
           if(PlayerFoot.position.y>transform.position.y){
                    box.enabled=true;
           }else{
                    box.enabled=false;
           }

}

As a side note, when using OnCollisionEnter, it is assumed that the collider has a rigidbody, so you can just refer to it as collision.rigidbody instead of collision.gameObject.GetComponent<Rigidbody2D>()

Hope this helps!