- Home /
Raycast/collision driving me insane - (never works correctly, any idea?)
Hi there!
I am facing a problem of raycast not working/behaving...in the game I am making (a FPS) when the weapon is fired (like a gun), the raycast are correctly sent out, but they do not seem to work 1/2 the time, with the collision, like when a ray is sent out (when you fire the weapon) and it should 'contact' on the ennemy; the ennemy should show a pain animation; and that is what happens when I use the melee weapon 'upclose'; but even there it work 1/2 the time, like the melee weapon connects, but only like in the middle of torso or something (like it seems the raycast 'gets it/collision body gets it' around the middle of the torso as if the middle collision is big enough to read raycast (The large circle around torso), it makes the pain animation 'sometimes' work...so it's really bizarre...like tehre is a problem of collision somehow;/either the raycast is not 'felt' by the collision boxes on the skeletel mesh of the ennemy...or I don't know.
Or maybe, the collision itself just not 'read' the raycast... also, I saw that if I would press the mouse button and not release it - then many raycast are sent out 'automatic machinegun style' and then the raycast 'finally' 'are felt' by the ennemy and he does the pain animation repeatedly...I did not press the mouse button several times...just holding it send non-stop raycast...that's bizar, I only wish to send 1 raycast not an automatic stream of them...it makes weird because the animation does not match, like the gun could be reloading or doing soemthing else, and teh raycast are sent out/doing damage (while reloading animation still playing) so it's off/timing is wrong. How do we make a raycast to fire once not a continuous firing-upon holding mouse button. Anyways I know this is complicated to untangle, I'm just wondering what does devs do to make the darn collision/raycast work/behave correctly instead of never working or 'sometimes' 'when it feels like it'...I just don't get it, most liklely a very advanced coding problem. I don't think it is a bug (though I could be wrong and indeed it is).
The ennemy has capsule colliders for each large member (like capsule for torso, capsule for forearm..etc, whole body) these capsules have a logic script that 'detects collision' (when firing the weapon/raycast), but therein the problem, the raycast never works right/or at all when firing a weapon or using a melee upclose weapon. It's like the body collision capsules are just not 'reading' the raycast right when colliding on body capsule...I wonder how big/how large they are suppose to be so that the 'raycast collides - the 1st time, each time' on the body capsules. I have tested the raycast on another non-ennemy/like a box crate, and the raycast is read correctly, like the collision box on the crate, reads the raycast right and the crate is broken after firing a few times on it - so it does work but not on the ennemy/I am guessing moving skeletal collision capsule is cause of problem...would buying a 'skinned mesh collider' asset solve this (the asset that makes the entire skinned mesh - be a 'animated' skinnedcollider itself). There is one large AI logic script for the ennemy and the script is put as 'collision' on the capsules so that the ennemy 'reacts' when shot at/pain anim (the collision check is that the script is on each capsule collider of the body - put as 'collision' so that it reads raycast). While the gun/melee weapons have a shooting script (gun/melee) to 'send out' raycast each time firing to 'do damage/health point loss' on the ennemy). Any help is greatly appreciated. Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613013908im_/https://answers.unity.com/themes/thub/images/avi.jpg)