- Home /
Collisions between two rigidbody's and scripting collisions problem
Hello everybody! Im having trouble and I would really appreciate the help.
I have an enemy character with a gun shooting bullets (bullets with rigidbody to apply force going 20f) and I want the bullets to hit my character (also with a rigidbody) and then destroy the bullets. I have this script attached to the bullet prefab:
using UnityEngine;
using System.Collections;
public class BulletHit : MonoBehaviour
{
private GameObject bullet;
// Use this for initialization
void Start()
{
}
void OntriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("BlueCube"))
{
print("Bullet hit!");
Destroy(bullet);
//GetComponent<AudioSource>().Play();
}
}
}
But it does not seem to register at all and the bullets do just bounce of my character. is it different because this script is attached to a prefab and not to a normal gameObject? I have a similar script on an other enemy that does not have a rigid body. Is the problem that both objects have a rigid body?
As I understand it, only one object of the two colliding can have a rigid body for functions like OnTriggerEnter and OnCollisionEnter. But what if I would want to make bullets that hit both static objects without a rigidbody and moving objects with a rigid body? That has to be possible, right? I would really appriciate it if someone could explain the exact interaction of rigidbody's and collision-detection to me.
Thanks in advance!
Answer by aditya · Sep 08, 2016 at 05:27 AM
Bro actually you have misspelled the collision function name ... it is OnTriggerEnter
and not OntriggerEnter
that letter T should be capital
Oh, yes! Thanks, man:). Now the script triggers! $$anonymous$$aybe this was it all along^^. But, strangely... Now the bullets go the other way when they are fired..
Oh, Yes! Thanks man! Now the script registers:).
Strangely, the bullets now go the opposite direction when firing... But tht is a different problem. Thanks for the answer^^.
Glad it helped ... would you please mark it as Answer now ^^
Answer by AurimasBlazulionis · Sep 07, 2016 at 05:30 PM
Physics in unity run at specified interval, which is I believe 0.02 seconds by default. Now think what happens when a bullet gets shot at very high speed. In one "frame" of physics, the bullet is in front off the wall, in the next one, it is behind it (flew through). Simply the physics engine does not think that you hit anything.
Now to the solutions:
Use Raycast. Using raycast you can have bullets behave like in Counter Strike. These are not physical bullets, instead it is just a ray which gets shot at a specified direction. You can use
RaycastHit
to do the checks for what the raycast hit and you also can set the maximum distance of the ray. Here is a code snippet from my scripts:RaycastHit hit; Physics.Raycast (transform.position, transform.forward, out hit)) { Do the stuff here }
DontGoThroughThings. That is if you really need physics bullets. Note, I have not tried this, but it is what you might want.
Collision detection mode can be used for quickly making a rigidbody not pass through things. You would need to use Continuous dynamic mode which makes bullets not pass through both static and dynamic objects. Do not use this everywhere. Over 100 objects with this setting can bring the performance down of the game, especially if no physx card is present.
Wow, that was fast! Thanks for answering!
But I do not thing that is the problem. $$anonymous$$y bullets are only moving at 20f, not very fast at all. What happens if I stand in front of them is that the bullets stop against my character and then resume their course if I remove the character. So there is a collision, but my script does not register it and does not work. But the answer below seemed to have partially solved it, it was a spelling mistake, woops..
But what you say is very good to know and i will use the information when making faster bullets! $$anonymous$$any thanks!