Home > Enterprise >  How to check if an object is colliding at the top or bottom part of a sphere
How to check if an object is colliding at the top or bottom part of a sphere

Time:11-11

I have a colliding object which can come from any direction, and I would like to check the position relative to the center of a sphere.

enter image description here

My idea was to create a vector with the same direction as the arrow that went through the center of the sphere and then check if the arrow position is over or under the new vector, but I don't know how to achieve this.

CodePudding user response:

You can get the point on impact. You can then measure the distance from the top and bottom of the sphere to the point of impact. The smaller distance is always the one it is closer to.

void OnCollisionEnter(Collision col)
{
    var top = transform.position   transform.localscale.y/2 * transform.up;
    var bottom = transform.position   transform.localscale.y/2 * -transform.up;
    
    var isTop = Vector3.Distance(top, collision.point) < Vector3.Distance(bottom, collision.point);
    
    if (isTop) {
        // It hit the top
        
    } else {
        // It hit the bottom
        
    }   
}

CodePudding user response:

You would need to define top and bottom better but if you just want to check in global space you can simply check whether the collision contact point is above or below the sphere center by comparing their Y axis value.

Something like e.g.

// Assuming this script is on the Sphere
private void OnCollisionEnter(Collision collision)
{
    // You could of course take all contact points into account and take an average etc
    // but to keep things simple just use only one for now
    var contact = collision.GetContact(0);
    var contactPoint = contact.point;
    var ownCenter = contact.thisCollider.bounds.center;

    if(contactPoint.y > ownCenter.y)
    {
        Debug.Log("Hit on top");
    }
    else
    {
        Debug.Log("Hit on bottom");
    }
}
  • Related