My 2D character is falling through the ground

Okay, so before you tell me this has been asked a thousand times, my question has a plot twist.

Here’s the summary. I’m trying the 2D Platformer tutorial: 1

I’ve done everything the way the guy says it should be except for the script. Which I altered a tiny little bit. The issue here is that my script makes the player fall through the platform but his works just as it looks on the video.

What I’d like to know is what the issue with my script is.

Before you ask, I’ve done everything else the way he said:

  1. I’ve added a Box Collider 2D to the platform.
  2. The sprite already comes with Rigidbody 2D, Box collider 2D, Circle Collider 2D components and with an empty groundCheck child that is used in the c# script. The Circle Collider 2D has its own physics material called player.
  3. The platform has the right tag and layer (Ground).

This is my script:

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

public class PlayerController : MonoBehaviour {

    [HideInInspector] public bool facingRight = true;
    [HideInInspector] public bool jump = true;
    public float moveForce;
    public float maxSpeed;
    public float jumpForce;
    public Transform GroundCheck;

    private bool grounded = false;
    private Animator anim;
    private Rigidbody2D rb2d;
    private float h;
     
    
    void Awake () {
        anim = GetComponent<Animator>();
        rb2d = GetComponent<Rigidbody2D>();
    }

   
	// Update is called once per frame
	void FixedUpdate () {
        
        h = Input.GetAxis("Horizontal");
        anim.SetFloat("Speed", Mathf.Abs(h));

        if (h * rb2d.velocity.x < maxSpeed)
        {
            rb2d.AddForce(Vector2.right * h * moveForce);
        }

        if (Mathf.Abs (rb2d.velocity.x)> maxSpeed)
        {
            rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
        }

        if (h > 0 && !facingRight)
        {
            Flip();
        }
        else if (h<0 && facingRight)
        {
            Flip();
        }

        if (jump)
        {
            rb2d.AddForce(new Vector2(0f, jumpForce));
        }

    }

    void Update()
    {
        grounded = Physics2D.Linecast(transform.position, GroundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
        
        if (Input.GetButtonDown("Jump") && grounded)
        {
            jump = true;
            anim.SetTrigger("Jump");
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

This is his:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    [HideInInspector] public bool facingRight = true;
    [HideInInspector] public bool jump = false;
    public float moveForce = 365f;
    public float maxSpeed = 5f;
    public float jumpForce = 1000f;
    public Transform groundCheck;


    private bool grounded = false;
    private Animator anim;
    private Rigidbody2D rb2d;


    // Use this for initialization
    void Awake()
    {
        anim = GetComponent<Animator>();
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

        if (Input.GetButtonDown("Jump") && grounded)
        {
            jump = true;
        }
    }

    void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");

        anim.SetFloat("Speed", Mathf.Abs(h));

        if (h * rb2d.velocity.x < maxSpeed)
            rb2d.AddForce(Vector2.right * h * moveForce);

        if (Mathf.Abs(rb2d.velocity.x) > maxSpeed)
            rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);

        if (h > 0 && !facingRight)
            Flip();
        else if (h < 0 && facingRight)
            Flip();

        if (jump)
        {
            anim.SetTrigger("Jump");
            rb2d.AddForce(new Vector2(0f, jumpForce));
            jump = false;
        }
    }


    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

I know the issue is going to be the most stupid thing, like I missed a semi-colon or something but I really can’t see what I’ve done wrong so I need a few extra eyes with fresh perspective.

Thanks a lot for helping.

Did you check your layer collisions? Under edit, project settings, physics 2d? I used this extensively in my app I just published. You can check it out, it’s called Microbz:

https://itunes.apple.com/us/app/microbz/id1418937151?ls=1&mt=8
https://play.google.com/store/apps/details?id=com.peaceofmindgames.microbz

You can see quite a bit of collision work, most of the magic was done using the layer collisions. Hope this was your problem.

Hi,
What happens if you disable PlayerController code cause i think problem isn’t from the code.
check two things.
1.there is no tick on any collider IsTriggers.
2.both platform and character has same z.(if your game is 3d).