- Home /
Physics.CheckBox not detecting collisions?
Hi,
So I am trying to detect if my player is on the ground (in Unity 2D). I initially used a raycast downward to detect the collider of the ground, but this does not account for collisions on the bounds of the players collider. What I mean by that is if the player is standing just off the edge where the center of the player is not directly above the ground, but the player's edge is still colliding with the ground, the code thinks the player is not grounded. So I tried using Physics.CheckBox to just check an area under the player. Below is the code I am using. I removed a lot of the clutter code just to show you guys how I am implementing this. I have the player starting at (0, 0), a long rectangle for the ground under him. Both objects have colliders and rigid body, remember the raycast did work. The raycast I used is commented out in the IsGrounded function so you guys can see that too. Any ideas as to why this is not detecting collisions?
Thanks
Zach
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
collide = GetComponent<Collider2D>();
distToGround = collide.bounds.extents.y;
}
private bool IsGrounded()
{
float distFromBoundToCenter = 0.02f;
//return Physics2D.Raycast(transform.position + new Vector3(0f, -distToGround-0.01f), new Vector3(0f, -1f), 0.01f);
return Physics.CheckBox(collide.bounds.center - new Vector3(0f, collide.bounds.extents.y + distFromBoundToCenter, 0f),
new Vector3(collide.bounds.extents.x, distFromBoundToCenter - 0.001f, 0f));
}
private void Update()
{
if (IsGrounded())
{
print("grounded");
}
}
}
Your answer
Follow this Question
Related Questions
Object with rigidbody bounces after hit. 0 Answers
Is Use Full Kinematics Contact bugged in 5.6.1? 0 Answers
How to send collision points to the physics engine? 2 Answers
Physics.CheckBox() Alternatives 0 Answers
Wind physics script 1 Answer