- Home /
How can i make only some Raycasts hit triggers
I'm sure there is an easy fix for this but i have no idea what it is, basically i use Raycasting for picking up items using triggers, and a separate Raycast for telling my game where to shoot.
The problem is that when Instantiating a decal, the shooter raycast hits the triggers and the decal hovers over some objects (sorry for the bad explanation, see picture below). Can i either make some Raycasts ignore triggers but not others or toggle the 'Raycasts hit trigger' option in physics settings via C#.
Thanks for any help.
Screenshot: http://s10.postimg.org/uvrmu86rt/Trigger_Issue.png
Answer by 767_2 · Nov 08, 2014 at 12:52 PM
you should make a layerMask and use it in your raycast like below , make a layer for objects you want to use them to trigger and say to use that layer on your raycast
(if new layer is layer 8)
int layerMask = 1 << 8;
if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)
I$$anonymous$$PORTANT EDIT: I looked into Layer$$anonymous$$asks and i dont know how it will help because the triggers are componants of GameObject.
Thanks for the answer, I have never used a layer mask before, would you be able to explain that a bit more thoroughly? do i put the layer mask on the my shooter raycast or my item raycast? and do i need to change the layers collision settings at all? Here is my Shooter raycast if it helps, the itempickup is much longer but similar:
RaycastHit hit;
Ray shootRay = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward, Color.red);
if(Physics.Raycast(shootRay, out hit) && Input.GetButtonDown("Fire1") && canFire && ammo > 0 && shooterWeapon && !inventoryOpen){
canFire = false;
ammo -= 1;
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
GameObject decal1;
if(hit.transform.tag != "Decal"){
decal1 = Instantiate(decal, hit.point, hitRotation) as GameObject;
decal1.transform.parent = hit.transform;
}
print ("BANG");
StartCoroutine(slightDelay(rof));
}
put all objects that you want to be in collison in your layermask and it doesnt got do anything with collision mask (look here)
sorry for the late reply, i dont know how it will help because i use those triggers for raycasting on another script, if i make a layer mask i wont be able to trigger them at all correct?