- Home /
Collider collision not being detected on fast passed objects
I have designed a health system for a zombie game i am intending to make, and want to have certain damage for certain body parts hit, for example, limb shot 25hp, body shot 45hp, head shot instant kill.... but i'm not sure at all where i am going wrong, i know what really screws me over is having to instantiate each object so i can't simply drag and drop the head to head and body to body in the hierarchy, I have to find each one individually through code and then use that Object to source a collider that is attached that object
So i have a bullet object with the tag of 'Bullet', it has a rigidbody and box collider.
I also have a Rigged Zombie tagged 'Zombie' which has an animator, Navigation script, navmesh agent, the zHealth script, a rigidbody and colliders marked as triggers on each shootable rig bone (Head, L Arm, R Arm, Body, R Leg, L Leg).
So what i would want to happen is i shoot the gun, the bullet is instantiated, bullet travels, hits target collider, deals damage then removes the bullet.
What actually happens, i shoot the gun, the bullet is instantiated, bullet travels, passes through target collider, bullet lands never to be found again, no damage dealt, no hit detected.
Coding Notes: Yes i understand that having bullets and bullet casings laying around that do not get removed if a collision is not met can be very demanding, but this is a part of the esthetic of the game, i will find a way for this to be less demanding and if all else fails then a system for when a certain number of casings/bullets are laying around the first bullet that was laying on the ground will be removed ect.
Added OnTriggerExit thinking the bullet may be traveling too fast for OnTriggerEnter to detect the collision.
Tried with and without else if () statements within OnTriggerEnter & OnTriggerExit.
Have had only one collision for a headshot and this was just after i figured out i needed a rigidbody on the zombie to detect collisions on child objects but after adding the rest of the body parts to the hit collision list, it some how stopped working.
Also undid all the coding to just after i added the rigid body and still never detected collision again.
I had a very basic healthsystem "working" earlier, but decided to re write it, because about 20% of direct hit collisions would detect, and overall was a lot more harsh on fps.
More notes may come, i just wanted to point out that i am only seeking help for why the bullets are not being detected, not criticism on the path i chose for an unique esthetic style game.
Current Coding:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zHealth : MonoBehaviour {
public bool dead = false;
public float zHlth;
private Rigidbody rig;
private chase aiScript;
private hitmarker hm;
private crosshair ch;
private GameObject player;
private GameObject zHead, zBody, zlArm, zrArm, zlLeg, zrLeg;
public Collider zHeadCol, zBodyCol, zlArmCol, zrArmCol, zlLegCol, zrLegCol;
private void Start()
{
aiScript = gameObject.GetComponent<chase>();
player = GameObject.FindGameObjectWithTag("Player");
ch = player.GetComponent<crosshair>();
hm = player.GetComponent<hitmarker>();
rig = gameObject.GetComponent<Rigidbody>();
zHead = transform.Find("_no_name_/Main__Hips_/LowerBack/Back/Chest/Neck").gameObject;
zHeadCol = zHead.GetComponent<SphereCollider>();
zBody = transform.Find("_no_name_/Main__Hips_/LowerBack/Back/Chest").gameObject;
zBodyCol = zBody.GetComponent<BoxCollider>();
zlArm = transform.Find("_no_name_/Main__Hips_/LowerBack/Back/Chest/LeftUpperArm").gameObject;
zlArmCol = zlArm.GetComponent<CapsuleCollider>();
zrArm = transform.Find("_no_name_/Main__Hips_/LowerBack/Back/Chest/RightUpperArm").gameObject;
zrArmCol = zrArm.GetComponent<CapsuleCollider>();
zlLeg = transform.Find("_no_name_/Main__Hips_/LeftUpperLeg").gameObject;
zlLegCol = zlLeg.GetComponent<CapsuleCollider>();
zrLeg = transform.Find("_no_name_/Main__Hips_/RightUpperLeg").gameObject;
zrLegCol = zrLeg.GetComponent<CapsuleCollider>();
}
private void OnTriggerEnter(Collider hit)
{
if (zHeadCol.CompareTag("Bullet"))
{
Destroy(hit.gameObject);
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zHead");
headShot();
}
else
if (zBodyCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zBody");
bodyShot();
Destroy(hit.gameObject);
}
else
if (zlArmCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zLeftArm");
limbShot();
Destroy(hit.gameObject);
}
else
if (zrArmCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zRightArm");
limbShot();
Destroy(hit.gameObject);
}
else
if (zlLegCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zLeftLeg");
limbShot();
Destroy(hit.gameObject);
}
else
if (zrLegCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zRightLeg");
limbShot();
Destroy(hit.gameObject);
}
}
private void OnTriggerExit(Collider hit)
{
if (zHeadCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zHead, Target Instantly Killed, Bullet Removed");
headShot();
Destroy(hit.gameObject);
}
else
if (zBodyCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zBody");
bodyShot();
Destroy(hit.gameObject);
}
else
if (zlArmCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zLeftArm");
limbShot();
Destroy(hit.gameObject);
}
else
if (zrArmCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zRightArm");
limbShot();
Destroy(hit.gameObject);
}
else
if (zlLegCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zLeftLeg");
limbShot();
Destroy(hit.gameObject);
}
else
if (zrLegCol.CompareTag("Bullet"))
{
hm.getHitMarker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zRightLeg");
limbShot();
Destroy(hit.gameObject);
}
}
public void headShot()
{
zHlth = 0;
}
public void bodyShot()
{
zHlth -= 45;
}
public void limbShot()
{
zHlth -= 25;
}
private void FixedUpdate()
{
if (zHlth <= 0)
{
dead = true;
rig.isKinematic = true;
rig.useGravity = false;
aiScript.isDead();
hm.died();
ch.died();
}
}
}
So you are using OnTrigger that means your bullet's collider is a trigger collider, right? Have you set the collision detection of your rigidbody to continuous/continuous dynamic?
I've checked your code. you're not using the parameters of OnTrigger
private void OnTriggerEnter(Collider hit)
{
if (**zHeadCol**.CompareTag("Bullet"))
{
Destroy(hit.gameObject);
hm.getHit$$anonymous$$arker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zHead");
headShot();
}
It should be
private void OnTriggerEnter(Collider hit)
{
if (**hit**.CompareTag("Bullet"))
{
Destroy(hit.gameObject);
hm.getHit$$anonymous$$arker();
hit.GetComponent<Rigidbody>().useGravity = false;
Debug.Log("Bullet has hit zHead");
headShot();
}
and by the code you provided, you have added multiple colliders. but these colliders would be considered as one collider for the parent gameobject.
Ah that was my mistake, I had changed the bullet from isTrigger to not (from changing things back and forth to try rectify the issue of not hitting 100% of the time), but have reverted back now, but still the collision is only detected about once every 10 direct hit shots, and yes as you were saying all the colliders clash, I could shoot the leg of the zombie, and I would get a message of "Bullet has hit zHead, Target Instantly $$anonymous$$illed, Bullet Removed", how can I make the collisions happen 100% of the time and how can I make individual impacts for different body parts ie individualise the colliders for arms legs, body and head?
I stumbled also on that problem as any collider in your child gameobject would trigger the parent's collision as if it was the parent's collider component. I haven't actually tried it yet but you can make a collision script for each body part and once it is hit and method/functions OnTriggerEnter() is called, then you call the methods/functions from your health script.
Answer by Injec · Oct 12, 2017 at 02:11 PM
If the bullet has a Rigidbody, set it's Collision detection to "Continuous Dynamic". And make your questions shorter please, i guess then you get more answeres ;)
Sorry this was not the issue, the bullet has always had a Continuous as well as Continuous Dynamic Collision Detection, because of its fast paced movement needing to be tracked continuously.
The reason my hits had stopped working though was because i changed the bullet Prefab to isTrigger = false when it most definitely should have been true, but i will try anything to fix an issue i'm having. but have had no avail hence why im here asking.
and everyone is different, i could put a 2 sentence question and it would be too short, and non descriptive, but then when you write a perfect description of everything that is going wrong and everything that you may think that could help towards a better answer, is going to be too long, just depends on the person answering weather they can be bothered to read long questions or weather they are genuinely in this just for the cookie points
Any trigger or collision detection you can replace with bullet/target coordinates. Say, if collision fails at some point, you run a function to check if the bullet was near. Not that perfect, but still a solution
Detection modes are actually not very good at detecting fast moving objects. Use a solution more like this: https://answers.unity.com/questions/1174027/object-moving-too-fast-so-that-the-collider-does-n.html?childToView=1627279#answer-1627279