#Raycast Hitting the backside of collider!
1 messages · Page 1 of 1 (latest)
Can you tell me how you know it's the backside?
Did you Debug the position?
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?
Welp, that's just the limit of physics calculations
The best approach would be to increase the size of the hitbox ig
Damn, I really wanted to add decal to where the player shot. So do you have any idea how that can be done?
Will retracing the ray back and finding a point on the collider a good idea? Not that i know how to implement that 😆
Actually, can you try Physics.SphereCast instead?
It might have a better chance to hit the collider correctly
I can't increase the size of the collider cause, I plan to convert the 3D model into a ragdoll once the zombie is dead
i will give it a try
thanks for the advice!
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
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.
@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
i have used Physics.SphereCast itself
if (Physics.SphereCast(PlayerEyes.position, 0.2f, Spray, out hitinfo, 1000, _enemy))
{
GameManager.Instance.ShowBloodParticleEffect(hitinfo);
hitinfo.transform.gameObject.GetComponentInParent<HealthManager>().TakeDamage(CurrentGunData.Damage , PlayerEyes.forward,hitinfo.rigidbody); //damage the enemy if shot
}
my code looks something like the above
And it still overshoot?
I am not familiar with this, i will try
yep
im am completely confused as to why
Is this in Update()?
yes
so did you try it already?
there is a function i made called Shoot() that does the raycasting
I DID!
and it FIXED THE PROBLEM!!!
THANK YOU SO MUCH @dusky wedge !!!
i was stuck on this for like 3 days 😆
why do you advise i shouldnt use it in the real game?
is it expensive?
yes it is expensive. now we atleast know its because it takes some time to the changes in navmeshagent position to update to the physics system
ohhhhh
btw do you have some animation on the zombie?
this is my first time working on a 3D game, i used to make 2D games before
yes
i play animation but i removed "Root Motion" so that it doesnt effect the navmesh traversal
once the zombie dies, i disable the animation component and activate forces on the ragdoll for the death
you could try to change the Update Mode on the Animator to Update Physics. that alone could potentially fix the problem, though I'm bit sceptical
let me give it a try
ok small update
SyncTransforms actually doesnt really fix the problem, it just makes the hitpoint more accurate, but sometimes it goes off and into the enemy body
I tried it, i dont see much of a difference 😢
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
as of now i have the zombie model and its animation as the child of the navmesh object
how deep in the enemy body does the hitpoint go? could it just be the colliders of the zombie representing the shape bit inaccurately?
this happens when i put the update mode to Animate Physics
i mean the animation update mode
if the animation update mode is Normal, then its all perfect
ah ok
so basically this problem is arising cause the physics engine and the unity game loop are not in sync right
then I guess the correct way would be to do this (so you don't need to call SyncTransforms) and keep the update mode to Normal
I will give it a try!
kinda... yeah. updating all the changes immediately to the physics engine would be bit too expensive I guess
or would it be better if i put the shoot logic in fixed update
idk how that would effect the gameplay tho
you could try but I'm afraid it won't still work. I think the changed transforms are not updated even every fixed update tho I'm not sure
ohhhh
Hey, i dont think this is possible, cause the animation is overriding the transforms
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
does the zombie have a rigidbody on it?
i trying to achieve ragdoll on it
so it has multiple rigidbodies on each of this limbs
but does the main body have a rigidobdy?
every red element you see on this image has a rigidbody
noope
Ok i added a rigidbod to the main body and tried
the problem persist
the raycast is still detecting inside the body
now that you have rigidbody on the body, you should move the body using rb.position and rb.rotation, not the transform ones
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!
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