- Home /
Bullets collide with a Trigger
Hi, i have a door with a trigger that activate animations only when the object tag "Player" collides with it, the problem its that my bullets for some reason collides with the trigger too, i dont know if the problem are on the bullets or on the door code.
this is part of the door code:
function OnTriggerEnter(hit:Collider){
if(hit.gameObject.tag == "Player"){
action = true;
}
}
function OnTriggerStay(hit:Collider){
if(hit.gameObject.tag == "Player"){
trigger = true;
}
}
The bullets are prefabs with box collider and rigidbody on it, and the shoot proyectile script is this (i get it from a tutorial):
var shotsound:AudioClip;
var shellPrefab : GameObject;
var proyectilevelocity:int;
var ammo:int;
var fireRate = 1.0;
private var nextfire = 0.0;
function FixedUpdate () {
if(Input.GetButton("Fire1") && Time.time>nextfire && ammo>0)
ShotProyectile();
}
function ShotProyectile(){
if(Time.time > nextfire)
{
var Grenade:GameObject = Instantiate(shellPrefab,transform.position,transform.rotation);
Grenade.GetComponentInChildren(Rigidbody).velocity =transform.TransformDirection(Vector3(0,0,proyectilevelocity));
//Physics.IgnoreCollision(shellPrefab.GetComponentInChildren(BoxCollider).collider,transform.parent.collider);
ammo--;
if(shotsound)
audio.PlayOneShot(shotsound);
Destroy(Grenade,10);
nextfire = Time.time+fireRate;}
}
Thanks for any help or idea.
Answer by Edyvargas · Nov 03, 2011 at 09:01 AM
UPDATE:
I found the problem!, was the DontGoThroughThings.js script, used to shoot fisic bullets at fast speeds and make collision always using a raycast on the bullet object itself (very useful script to solve the missed collisions problem on fast movement objects).
Without this script attached to the bullet, the shooting through triggers behavior is normal, just the problem of some missed collisions if the bullet goes too fast, if someone have an idea on how to solve this on the mentioned script, you can see it on the upper link, thanks for the help.
Oh, that thing! Yeah, it uses raycasts to manage its inbetween bits, so my solution about the raycasts should fix it. Or, just make sure that the layermask on it doesn't include the layer that your triggers are on.
You should have mentioned that you were using that script! It explains everything.
Yes, i forgot that detail :)...., the layermask excluding the door (the part with the trigger) layer works O$$anonymous$$, and keeping the other door parts on the default layer, but the collisions with the other parts of the door, sometimes makes a funny bullet behavior, like if the bullets where floating when collides...but just sometimes...
Basically, if you follow the tips in my answer, it automatically fixes the problems with DontGoThroughThings, and keeps all of its advantages. So go do that. $$anonymous$$ostly, the 'Raycasts Hit Triggers' bit should fix it.
You are right, thanks, disable the "Raycasts Hit Triggers" solve the problem at all, and i found that the funny floating bullets, appears on some collisions, even without the DontGoThroughThings script...but only with capsule type bullets, with cube bullets all works like a charm..., interesting...
UPDATE: If i let the "Raycasts Hit Triggers" on, and use a specific layer for all the triggers, and then add this layer to the layermask and avoid collisions to it, has a strange effect on the cube type bullets too, and make them floating a bit sometimes when hits on a collider.
Answer by syclamoth · Nov 03, 2011 at 05:40 AM
There are a few ways of fixing this. The simplest one is to just go to your Physics settings, and disable 'Raycasts Hit Triggers'. If that's not an option (because it'll stop other things from working properly), you can put all your triggers on a "triggersOnly" layer, and then use a layermask on your bullet raycasts to stop them from colliding with your triggers.
EDIT: Oh wait, you're using rigidbodys for your projectiles? In which case I can't really explain why they're colliding with the trigger. Triggers should never interact with rigidbodies, other than to send messages, and you're already checking for player tags in your OnCollision bits. Otherwise, go to your Physics settings (again) and set up the collisions matrix so that projectiles never collide with trigger volumes (somehow).
Hi, thanks for the help, you are right, i dont know either why the bullet prefabs (with rigidbody) are colliding with triggers, but i will find out somehow, i think there is nothing to do with the door script, beacuse the same happens with other triggers.....
Your answer