Check collision above player with raycasthit2d array

Hi Guys,

I’m having an issue with my collision detection.

I followed one of the proper Unity tutorials on YouTube and have made a character physics script with a controller.

This works nicely and the Ground Check (even on slopes) works fine.

The problem is I need to be able to detect which side of the block/background the player sprite has hit.

I tried doing this using the below

if (hitBufferList*.point.t > rb2d.position.y)*

{
Debug.Log(“Hit Head!”);
}
This is done but a function that uses Rigidbody2D.Cast to get the collisions into a List
I then loop through and check with a move function.
I don’t was under the assumption RaycastHit2D.point was the world co-ordinate of the collison point. So if this was higher than the player it would be above them.
Weirdly it works the other way around (if less than) showing the collision was below the player.
Is there a way to determine the side of collision using normalized vectors? I.e if the normalised vector is negative or postive I could determine the direction?
I can share more code if that helps?
Cheers
James

Turns out it was a simple solution.

I had placed the if statement in the wrong loop :frowning:

    void Movement(Vector2 move, bool yMovement)
    {
        float distance = move.magnitude;

        if (distance > minMoveDistance)
        {
            int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);

            hitBufferList.Clear();
            for (int i = 0; i < count; i++)
            {
                hitBufferList.Add(hitBuffer*);*

}

for (int i = 0; i < hitBufferList.Count; i++)
{
Vector2 currentNormal = hitBuffer*.normal;*
Collider2D collider = hitBufferList*.collider;*
if (currentNormal.y > minGroundNormalY)
{
grounded = true;
if (yMovement)
{
groundNormal = currentNormal;
currentNormal.x = 0;

}

}
float playerpos = rb2d.position.y;
if (hitBufferList*.point.y > playerpos)*
{
velocity.y = velocity.y - 2;

if (collider.tag == “Blocks”)
{
if (blockHit == false)
{
SpriteRenderer sprite = collider.GetComponent();
blockHit = true;
StartCoroutine(blockBounce(sprite.transform));
}
}

}

float projection = Vector2.Dot(velocity, currentNormal);

if (projection < 0)
{
//velocity = velocity - projection * currentNormal;
}
float modifiedDistance = hitBuffer*.distance - shellRadius;*
distance = modifiedDistance < distance ? modifiedDistance : distance;
}
}

rb2d.position = rb2d.position + move.normalized * distance;
}
This code then worked perfectly. If the hitBuffer point.y is greater than (above) the player, then I have hit a block above me.
This then runs a test script to bump the block up (like in Super Mario Bros.).
I should remove the projection part as I am no longer using that.
James