Collision problems with jump through platform 2D platformer

Hey! I have created a platform and attached a 2dboxcollider which is used by effector and a platform effector 2D. This is the script of the platform.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class verticalplatform : MonoBehaviour
{
    private  PlatformEffector2D effector;
    private float waitTime;
   

   

    void Awake()
    {
        effector = GetComponent<PlatformEffector2D>();

    }

    void Update()
    {

        Debug.Log(isOnPlatform);
        if (Input.GetKeyUp(KeyCode.DownArrow))
        {
            waitTime = 0.01f;

        }




        if (Input.GetKey(KeyCode.DownArrow))
        {
            if (Input.GetKeyDown("space"))
            {

                if (waitTime <= 0)
                {
                    effector.rotationalOffset = 180f;
                    waitTime = 0.01f;
                }

               

            }

            else
            {
                waitTime -= Time.deltaTime;
            }

           


        }

       
        if (Input.GetKeyDown("space") && !Input.GetKey(KeyCode.DownArrow))
        {
            effector.rotationalOffset = 0;
           

        }
       

    }





}

The problem is that if I have several platforms in a row ( vertically) and the player is on the top one. When i jump off the platform (throught the platform) the player will ignore ALL the platforms. So the script works for all the platforms at the same time. I tryed to play around with the script by adding “this” and static but nothing worked. Maybe its something trivial that im missing?

I’m no expert, and I’m not fully understanding what you’re trying to accomplish, but instead of using a wait time check, try a velocity on y check. I just did this myself to disable a ground check/“OnLand” event.

I don’t know, it might not work if your character has y movement while moving normally though. But maybe adding a tolerance to the last “else” statement (such as, else if verticalVel >= 1…)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class verticalplatform : MonoBehaviour
{

Rigidbody2D rb; // define Rigidbody2D as rb

public float verticalVel; // make a public float called verticalVel

void Awake()
{

rb = GameObject.FindWithTag("Player").GetComponent<Rigidbody2D>() // define what rb points too

}


void Update()
{

verticalVel = rb.velocity.y; // what does verticalVel actually equal, in this case, movement on y axis (up/down)

if (verticalVel <= 0) // if verticalVel is less than or equal to 0 (meaning no movement or downward movement)
{

effector.rotationalOffet = 180f // turn on the effector

}

else effector.rotationalOffset = 0 // otherwise....

}


}

Oh, and this might be a dumb thought, but have you clicked “One-Way” on the effector properties?
That will serve the purpose of moving through the platform (but only one way). And which ever way the effector offset is pointing, is the way that you can’t pass though.

I guess I’m wondering the ultimate reason why you need to disable the offset

One - way is marked. The problem with velocity.y might be that maybe i dont have to fall down in order to touch the platform. I will attach an image of my problem

Oh, I get it now. Yes, my idea wouldn’t work at all. I understand.

Maybe this is not the most efficient way, and there is probably a way to improve it, but I would do a coroutine. Every time you press the down key, the coroutine runs.

It could be real simple, and it would eliminate your “wait time” float, as the coroutine has a method of waiting within it. This would need to go on the Verticalplatform Script, after Update. And to run this co routine, just put under your GetKeyDown

 StartCoroutine(DisableEffector());

Here’s the co routine code.

   public IEnumerator DisableEffector() // we are defining a new function using an IEnumerator, called DisableEffector. IEnumerator is a method for getting/storing numerators/sequences.
    {

        //below instructions happen in order

            effector.rotationalOffset = 180f;
            yield return new WaitForSeconds(0.02f);
            effector.rotationalOffset = 0f;
        }

Also, maybe instead of changing the effector, it would also disable the collider. This should work the same, you would just need to replace the effector off/on portions with Collider off/on.

Interesting! Never used such thigns before. Gonna try it out tonight! Gonna let u know if it worked :smile:
Thanks!