Character Falls Through Ground

I know this is a common issue, I’ve looked everywhere and can’t find a solution. I have a character with a RigidBody2D as it’s collider, and a block object with a BoxCollider2D. I have tried setting it to trigger, not trigger, either way it just falls right through. My block has the tag “ground” set on it, but it seems like either way it’s not detecting the collision(I debugged by having it print upon collision enter). Here’s my code, sorry if it’s sloppy, took it from another game I’m making lol.

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

public class Player : MonoBehaviour
{
    public Magic[] Magics;
    public bool isGrounded;
    public Rigidbody2D rb;

    public float walkSpeed = 7;
    public float runSpeed = 10;
    public float gravity = -12;
    public float jumpHeight = 1;
    [Range(0, 1)]
    public float airControlPercent;
    public float screenHalfWidthInWorldUnits;

    public float turnSmoothTime = 0.2f;

    public float speedSmoothTime;
    float speedsmoothVelocity;
    float currentSpeed;
    float velocityY;
    float velocityX;


    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "ground")
        {
            isGrounded = true;
        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        isGrounded = false;
    }
    public void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            foreach (var spell in Magics)
            {
            }
        }
        float inputX = Input.GetAxisRaw("Horizontal");
        float velocityX = inputX * walkSpeed;
        transform.Translate(Vector2.right * velocityX * Time.deltaTime);

        if (transform.position.x < -screenHalfWidthInWorldUnits)
        {
            transform.position = new Vector2(screenHalfWidthInWorldUnits, transform.position.y);
        }
        if (transform.position.x > screenHalfWidthInWorldUnits)
        {
            transform.position = new Vector2(-screenHalfWidthInWorldUnits, transform.position.y);
        }

        Vector2 input = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        Vector2 direction = input.normalized;

        bool running = Input.GetKey(KeyCode.LeftShift);

        float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
        animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
        if(isGrounded == true)
        {
            rb.gravityScale = 0;
        }
        else
        {
            rb.gravityScale = 1;
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            Jump();
        }

        void Jump()
        {
            if (isGrounded == true)
            {
                float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
                velocityY = jumpVelocity;
            }
        }
    }
    float GetModifiedSmoothTime(float smoothTime)
    {
        if (rb.gravityScale > 0)
        {
            return smoothTime;
        }
        if (airControlPercent == 0)
        {
            return float.MaxValue;
        }
        return smoothTime / airControlPercent;
    }
}

Thanks in advance.

Rigidbody2Ds are not colliders. To have a chance at this working you need to have some type of Collider2D on the thing with the Rigidbody2D.

You also need to make sure at spawn they are not already overlapping by too much. See enclosed package. Those are 3D objects but it’s all 2D colliders underneath it.

5947646–637001–Collide2D.unitypackage (3.66 KB)

1 Like

Ahh, I got it now! I didn’t think rigid bodies were colliders but it gave me an error whenever I tried to add the collider… Turned out, I wasn’t adding the 2D collider, but the 3D one lol. Thanks, I got it now

1 Like