Trying to allow my player to pass through a platform and then be able to land on it.

So I’m trying to write a script that allow my player to jump from under a platform and go through it, but then land on it when falling down. I have used on Trigger Enter and Exit to control the detection but I have been unsuccessful. Below is my code and what I have tried so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformMovement : MonoBehaviour
{
private float forceSpeed = 5;
public bool ignoreCollision = false;
private Rigidbody platformRb;
private PlayerMovement playerMovementScript;
private Collider playerCollider;
private Collider platformCollider;

// Start is called before the first frame update
void Start()
{
platformRb = GetComponent();
platformRb.AddForce(Vector3.left * forceSpeed, ForceMode.Impulse);
playerMovementScript = GameObject.Find(“Player”).GetComponent();
playerCollider = playerMovementScript.GetComponent();
platformCollider = GetComponent();
}

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

}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag(“Player”))
{
ignoreCollision = true;
Physics.IgnoreCollision(playerCollider, platformCollider, ignoreCollision);
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag(“Player”))
{
ignoreCollision = false;
Physics.IgnoreCollision(playerCollider, platformCollider, ignoreCollision);
}
}
}

If anyone has an idea on how to fix this problem, it would be really nice to hear. Thank you!

2 Likes

Disable platform’s collider if the player is below it, and enable if it is above. Thigs will get more complex if there are other enemies, though.

1 Like

Physics.IgnoreCollision helps in that case where only your specific player should affect the collision with the platform and not enemies:
https://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
Extra tip: For performance reasons in case of larger levels, also add an X-coordinate check (with leeway for the player size) so you do not enable\disable collisions of hundreds of platforms all the time.

It’s really cool that Unity has a built in feature for all this however as seen in the vid.

1 Like