I have a problem where the ground check method isn’t working on edge colliders. Every time the player collides on a box collider 2D it works fine but when it collides on an edge collider the grounded checkmark is still false? And yes I have the Ground tag on both of them as well.

Is this some kind of bug?

send the ground check code, and where is the edge collider on? on the player?

```
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragNShoot : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
public Animator anim;
public Vector2 minPower;
public Vector2 maxPower;
public Vector3 screenPosition;
private SpriteRenderer spriteRenderer;
public bool grounded;
TrajectoryLine tl;
Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;
private void Start()
{
cam = Camera.main;
tl = GetComponent<TrajectoryLine>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
private void Update()
{
screenPosition = Input.mousePosition;
if (Input.GetMouseButtonDown(0) && grounded)
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
anim.SetBool("preparing", true);
}
if (Input.GetMouseButton(0) && grounded)
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}
// We released the rodent
if (Input.GetMouseButtonUp(0) && grounded)
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
anim.SetBool("preparing", false);
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode2D.Impulse);
tl.EndLine();
}
// Air Force
if (Input.GetMouseButton(0) && screenPosition.x < 985f && !grounded)
{
rb.AddForce(new Vector3(3f, 0f, 0f));
}
else if (Input.GetMouseButton(0) && screenPosition.x > 985f && !grounded)
{
rb.AddForce(new Vector3(-3f, 0f, 0f));
}
// Sprite Flip
if (Input.GetMouseButton(0) && screenPosition.x > 985f && grounded)
{
spriteRenderer.flipX = true;
}
else if (Input.GetMouseButton(0) && screenPosition.x < 985f && grounded)
{
spriteRenderer.flipX = false;
}
}
[B] private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
Vector3 normal = other.GetContact(0).normal;
if (normal == Vector3.up)
{
grounded = true;
anim.SetBool("flying", false);
}
}
}
private void OnCollisionExit2D(Collision2D other)
{
grounded = false;
anim.SetBool("flying", true);
}
}
```

[/B]

I would be highly skeptical of this construct, testing for equality:

If the ground is even slightly off or something is odd with the contact point, the above won’t be true.

Floating (float) point imprecision:

Never test floating point (float) quantities for equality / inequality. Here’s why:

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

https://discussions.unity.com/t/851400/4

https://discussions.unity.com/t/843503/4

“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums

If you just need to see if the normal “uppish enough” then check to make sure its y component is sufficiently positive.

And it definitely works, colliding with an edge collider.

If you’re going fast, make sure you have continuous collision detection enabled on the Rigidbody2D.

Put the above script on both sides of the collision.

```
using UnityEngine;
// @kurtdekker
public class Report2D : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("OCE2D: I am " + name + ", hit by " + collision.collider.name);
}
}
```

Probably a good situation to use a dot product: https://docs.unity3d.com/ScriptReference/Vector3.Dot.html

Just need to test if the dot product of the normal and `Vector3.up`

is close enough to 1.

Squints closely at monitor… reads the text a few more times…

Waaaait, isn’t the .y component of a 2D vector actually equal to the dot product of that vector and `Vector3.up`

?

Well if it is I didn’t know that. I don’t know how the maths works. I just know the behaviour of the results.

1 Like

Fair enough… but it’s kinda cool tbh.

Shortest most concise blurb I found was this:

if we have matrices A = [a b] and B = [c d], the dot product would be calculated as (a*c) + (b*d).

So if the normal is [x y] and B is Vector2.up [0 1] then result is: x * 0 + y * 1 == y

1 Like