I want to make my bullet can enter my object to play some function and I am using box collider. Here is my code :
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "shot"){
move = false;
hurt = true;
aware = true;
health--;
Destroy(other.gameObject);
}
}
That function is not always played when my bullet pierce it. The function is always played when I make bullet speed become slow, but I need fast bullet.
1 Answer
1
SO what your saying is, You want the bullet to “hit” the collider and when it detects collider hit to call a function from script? If so, then only thing I can suggest is, make the collider slightly smaller than the game object instead of the same size as. So, the bullet will seem to go through the “skin” of the game object before crashing into the collider inside the game object. The bullet will destroy itself as per the function. I would just try and resize the collider about maybe 60-70% of the game objects size and then reset its position to the center of the object itself. That should work, Ill test it out and see.
The bullet should have IsTrigger set to true. The OnTriggerEnter function loooks ok to me, but maybe the bullet moves too fast for the rigidbody to register the collision; this is often a problem with fast-moving physics objects. You could also try using OnTriggerStay instead. Lastly, make sure that the player only touches one trigger object (the bullet) because the OnTrigger... function will only register one other object, and if that "slot" is already taken, the bullet collision won't register.
– ChernoDoesn't work, even I have used OnTriggerStay. The object in this case is the enemy and the player is fps controller. I set IsTrigger to false but Ontrigger function doesn't work, the bullet is like pushing the enemy.
– Robby-rEINEWALDif(other.gameObject.tag == "shot"){ take out one of the = and see if that fixes the problem?
– SnStarrit makes the game can't be run.
– Robby-rEINEWALDEliminating one of the "=" is wrong and in fact can "harm" your code. Be aware that for collision, including triggers, to work, at least one of the involved colliders needs to be convex.
– Cherno