Skip to main content
Bumped by Community user
added 177 characters in body
Source Link
MBrain
  • 75
  • 1
  • 10

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..

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.

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..

Source Link
MBrain
  • 75
  • 1
  • 10

Unity3d OverlapSphere and raycasting problem

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.