- Home /
How to make an enemy recognize a bullet
i have tried to make a trigger collider around the enemy, but it won't always recognize the bullet. how else can i do this?
Because colliders sometimes won't recognize collisions at high speeds, it's better to use Raycasting. Some info about Raycasting is here:http://docs.unity3d.com/ScriptReference/Physics.Raycast.html Best of luck. Feel free to ask any questions about Raycasting if you decide to use it.
i tried that too, but i'm pretty sure recasts return a boolean of wether there is something in front of it or not. i need the script to know the specific game object so that i will damage that enemy, not some random one.
You can retrieve whatever object was touched by the raycast
http://docs.unity3d.com/ScriptReference/RaycastHit.html
RaycastHit hit; if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F)){ hit.collider }
Answer by MrSoad · Oct 27, 2014 at 01:30 PM
Do something like this for finding out which object you have hit, read the comments as this won't just cut and paste due to the raycast parameters used in this example.
function subDetect_Bullet_Hit() {
var hit : RaycastHit;
var oOther_Object : GameObject;
//These parameters are from something I did, not instantly transferable to your project and needs
if (Physics.Raycast (transform.position, transform.forward, hit, fRaycast_Distance, lmPlayer)) {
if (hit.collider.gameObject.tag == "Your_Target_Object_Tag") {
//Do you what you want now you know it has hit what you want.
//You can store this gameobject, find its components and scripts etc,
//and do whatever you need with it, inc destroy it.
//Store like this :
oOther_Object = hit.collider.gameObject;
}
}
}
Can't you try yourself? Would be a little bit of learning.
it doesn't seem to work. is there an option for debug that visually shows me the raycast? it'd be really handy
Thanks dude! sorry it took so long to accept your answer, but i had to tweak it a lot to get it to work
Your answer