- Home /
Raycast with doubleCollider
Hi,sorry for my english. I am building a third person shooter. On enemy target i have a double collider, Capsule collider for the enemy mesh and a sphere collider for the aggrozone(on the same object(enemy)). When i shoot of course my raycast hit the aggrozone and decrease the enemy's life. How can ignore the first collider?Please help me, i am going crazy. Ray ray=Camera.main.ViewportPointToRay(Vector3.one*0.5f); Debug.DrawRay (ray.origin, ray.direction * 100, Color.red, 2f); RaycastHit hitinfo;
if (Physics.Raycast (ray, out hitinfo, 100)) {
var health = hitinfo.collider.GetComponent<health> ();
if (health != null) {
health.takedamage (damage);
print(hitinfo.collider.gameObject.name);
}
else print(hitinfo.collider.gameObject.name);
Answer by Captain_Pineapple · May 31, 2018 at 10:43 AM
Hey there,
shouldn't an aggrozone be a trigger instead of a collider? Then a normal raycast should ignore it either way. If this does not help or is not an option let me know. There are some other ways that might be working for your situation.
Thanks for reply. This is my settings on enemy player. I am a beginner on Unity.
Here my aggro code: public class aggrodetection : $$anonymous$$onoBehaviour {
private health myhealth;
public event Action<Transform> onaggro= delegate {};
private void OnTriggerEnter(Collider other){
var player = other.GetComponent<playermovement> ();
if (player != null) {
onaggro (player.transform);
Debug.Log ("Aggrod");
}
}
}
U were right! A normal raycast should ignore trigger collider if u go in : Edit->ProjectSettings->Physics->Uncheck m_Querieshittrigger. Thanks a lot, now works!
Hey,
yeah my bad on that one. I forgot that you have the control to enable or disable this for your whole project and that the default value is enabled.
You can ofc tell a specific raycast to hit triggers or not via the QueryTriggerInteraction enum as parameter for the Physics.Raycast.
Answer by AlbertoForlenza · May 31, 2018 at 11:30 AM
I just wanna figure it out. I can poste everything , scripts etc.
Answer by Axtrainz · May 31, 2018 at 12:40 PM
@AlbertoForlenza Separate aggro-zone collision on a child object with different tag/layer, Also make the Ray Mask to only hit the enemy tag or layer, not all.
you can check something like this:
if(hitinfo.collider.gameobject.tag == "Enemy")
{
takedamage();
}