#Raycast Hitting the backside of collider!

1 messages · Page 1 of 1 (latest)

proven bane
#

I currently working on a FPS game where I have player who can shoot a gun via a raycast, The issue is that the raycast when it hits the player, it detects the collision on the backside of the collider while passing through the front side.

Does anyone have idea why it happens, or how to debug the same?

modern drum
#

Can you tell me how you know it's the backside?
Did you Debug the position?

proven bane
#

turns out that it wasnt backside, but rather that the hitpoint was further away

#

allow me to send some pictures for claritty

#

when I shoot the zombie and use the raycasthit to obtain the hitpoint and normal of the raycast. When the zombie is moving fast, the hitpoint seems to come behind the zombie
Allow me to show some picture to drive my explanation.
as we can see above, I drew a Gizmos Line from player's gun to the raycasthit.point and it comes to be behind the zombie
and this is when the zombie is at speed 10 (im using a navmesh agent)

#

exact same scenario but the speed of the zombie as been ramped up to 20

#

I want to add blood decal effects to where the player shot, but with raycast, I am not able to achive this functionality
Can anyone point out, what could be the potential issues?

modern drum
#

Welp, that's just the limit of physics calculations

#

The best approach would be to increase the size of the hitbox ig

proven bane
modern drum
#

Actually, can you try Physics.SphereCast instead?
It might have a better chance to hit the collider correctly

proven bane
proven bane
proven bane
#

i tried with spherecast

i drew a ray from player to raycasthit.point and drew a sphere a radius of the spherecast

#

the same issue still seems to persist

#

same scenario but the zombie's speed is made twice as fast

modern drum
#

I meant using SphereCast instead of RayCast, not Physics.OverlapSphere
SphereCast shoots a sphere instead of a ray, so it has a greater chance to collide properly.

dusky wedge
#

@proven bane have you tried calling Physics.SyncTransforms() before doing the cast? Im not saying you should do that in the actual game but it would be a way to varify where the problem is

proven bane
proven bane
modern drum
#

And it still overshoot?

proven bane
proven bane
proven bane
#

yes

dusky wedge
proven bane
proven bane
#

and it FIXED THE PROBLEM!!!

#

THANK YOU SO MUCH @dusky wedge !!!
i was stuck on this for like 3 days 😆

proven bane
dusky wedge
proven bane
#

ohhhhh

dusky wedge
#

btw do you have some animation on the zombie?

proven bane
#

this is my first time working on a 3D game, i used to make 2D games before

proven bane
#

once the zombie dies, i disable the animation component and activate forces on the ragdoll for the death

dusky wedge
proven bane
#

let me give it a try

proven bane
proven bane
dusky wedge
#

if that doesn't work, only better option (than Physics.SyncTransforms) that comes to my mind is to separate the Nav Mesh Agent and the visual zombie mesh with the colliders completely. the way I'd approach that would be to remove the Nav Mesh Agent component from the zombie, make a new empty gameobject with that Nav Mesh Agent component. now the zombie is not moved by the Nav Mesh Agent, the agent just moves by itself. to make the zomvie follow the agent again, you can set zombieRigidbodyWhatever.position = navMeshTransform.position (same for rotation). when you set rigidbodys position directly, it should immediately update to the physics system unlike navmesh does

proven bane
#

as of now i have the zombie model and its animation as the child of the navmesh object

dusky wedge
proven bane
#

i mean the animation update mode

#

if the animation update mode is Normal, then its all perfect

dusky wedge
#

ah ok

proven bane
#

so basically this problem is arising cause the physics engine and the unity game loop are not in sync right

dusky wedge
proven bane
#

I will give it a try!

dusky wedge
proven bane
#

or would it be better if i put the shoot logic in fixed update

#

idk how that would effect the gameplay tho

dusky wedge
proven bane
#

ohhhh

proven bane
#

i tried doing what you did

#
[SerializeField] Rigidbody _enemyRoot;
    private void Update()
    {
        Debug.Log(transform.position);
        _enemyRoot.transform.localPosition = transform.localPosition;
        _enemyRoot.rotation = transform.rotation;
    }

i attached a script to the navmesh and did this

#

and the zombie is just stuck at where it is

#

but the navmesh is moving around

dusky wedge
#

does the zombie have a rigidbody on it?

proven bane
#

i trying to achieve ragdoll on it
so it has multiple rigidbodies on each of this limbs

dusky wedge
#

but does the main body have a rigidobdy?

proven bane
#

every red element you see on this image has a rigidbody

proven bane
proven bane
#

the problem persist

#

the raycast is still detecting inside the body

dusky wedge
#

now that you have rigidbody on the body, you should move the body using rb.position and rb.rotation, not the transform ones

proven bane
#

oh ok my bad

#

let me try again

#

IT WORKKSSSSSS

#

your solution fixed the problem

#

now im getting decals properly as to where the player shot

#

Thanks a lot @dusky wedge !

#

Thank you @modern drum as well!

dusky wedge
#

no problem, keep in mind that you have now added rigidbody to the main body. you may need to set that as kinematic when the ragdoll is enabled so it doesn't get messed up

proven bane
#

Yep sure!

#

thanks again

#

i guess now i will have to close this post

#

Untill we meet again!