0
\$\begingroup\$

I have a grenade (rigidbody) which make a spherecast when it detonates. On each found colliders, i will make a raycast to check if it is visible to the grenade or not.

void Explode() {       

    Collider[] colliders = Physics.OverlapSphere(transform.position, radius);  

    foreach(Collider coll in colliders) {             

        // when the collider belongs to a player.. 
        if(coll.transform.GetComponent<Player_Controller>() != null) {                               

            RaycastHit hit;

            // ..i want to check whether its behind an obstacle or not
            if(Physics.Raycast(transform.position, (coll.transform.position - transform.position), out hit)) { 

                coll.transform.GetComponent<Player_Controller>().TakeDamage();

            } 

        } 

    }

    Destroy(gameObject);

}

My problem is i will get the collider for all players, but when i do the raycast from the grenade to the found collider, i only hit the environment i play in. So basically my map. The grenade is laying on the ground so maybe there is an error in my raycast direction.

Edit: I solved this, the raycast was pointing not at the very bottom of the collider instead a little bit below so it was under the ground. I havent seen it unless i draw a debug ray..

\$\endgroup\$
  • 3
    \$\begingroup\$ If you have found an answer, write an answer and mark it accepted. \$\endgroup\$ – trollingchar May 30 at 11:39
1
\$\begingroup\$

The raycast was pointing not at the very bottom of the collider instead a little bit below so it was under the ground. I havent seen it unless i draw a debug ray. Because my character has an empty gameobject representing its head, i point at this instead to be sure its above the ground.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.