Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Physics.CheckSphere get collision GameObject

Discussion in 'Scripting' started by krocky4234, Jun 22, 2020.

  1. krocky4234

    krocky4234

    Joined:
    Jun 18, 2020
    Posts:
    1
    Hello,
    How can i get the Gameobject the Sphere (Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask)) is colliding with? I have tried with the OnCollisionEnter(Collision collision) function but i didnt work out. Is there a was to get the collision GameObject?,Hello,I am currently checking a collision with a wall like this: isWallJump = Physics.CheckCapsule(WallCheckBottom.position, WallCheckTop.position, 1f, groundMask); but i need the GameObject that im colliding with.
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    The
    Physics.Check*
    methods are only for checking if something exists, not what. Use the
    Physics.Overlap*
    methods to know what, they return an array of the colliders.

    E.g. Physics.OverlapSphere or Physics.OverlapCapsule.
     
    aaronrums and krocky4234 like this.
  3. vanceagrig

    vanceagrig

    Joined:
    Jul 13, 2018
    Posts:
    7
    Thank you Adrian, that helped, a lot.
     
  4. Kordsky

    Kordsky

    Joined:
    Nov 12, 2020
    Posts:
    1
    I had same problem and i solved it tanks to adrian so here is my working code for groundCheck if there is anybody who have same problem :

    public bool isGrounded;

    //Put this into Update()
    Collider[] hitcollider;
    hitcollider = Physics.OverlapSphere(groundCheck.position, groundDistance);
    if(hitcollider.Length > 0)
    {
    isGrounded = true;
    }
    else
    {
    isGrounded = false;
    }