In my game, you can shoot these arrows and when they hit a wall, they stick to it. But as you can see here: Imgur: The magic of the Internet , every time an arrow hits the wall, it hits it differently, some disappear inside it and others don’t even touch it. I’ve tried a lot of things, using OnTriggerEnter function with the collision detections set as continuous, raycasting(the one used in the example), and translation with raycasting(that showed the best results). But even so, the collisions are still not constant. Any thoughts on what I can do or if there’s something to be done?
Using the OnTriggerEnter method, I tried changing the rigidibody body type or just setting the rigidibody velocity to zero, neither of them showed the desired results
Rigidbody movement happens in FixedUpdate. Update is not the right time to do your raycasts
This code only makes the arrow static. That will freeze it in place wherever it was when you performed the raycast. But this will always be before it actually hits the wall. But the point of the raycast is to find a wall so that you can stick the arrow in the wall. You need to move the arrow to the hit point of the raycast in addition to making it static. something like:
What are you using for rayDistance? It should be the amount of distance the arrow is about to travel in one physics step. Something like: rb.velocity.magnitude
Yes. Velocity is distance * time. It describes how far the projectile is going to move per second. FixedUpdate runs at a fixed interval of time (usuallly 50 times per second). So if you multiply the rigidbody velocity times the physics timestep (Time.fixedDeltaTime) you will get the distance that the projectile is going to travel in the next physics update. The point is to see if the projectile will hit a wall in that time. If it will, you can stick the projectile to that exact point where it would collide.
I’m surprised the code you posted above is working because you should probably actually use this for the hit distance to be more accurate: rb.velocity.magnitude * Time.fixedDeltaTime, since that is actually the distance the arrow will travel in the next physics update. The code you currently have is probably sticking into the wall a bit too early.
Also, I think you don’t need to set rb.velocity each time. You can just set it once when you fire the arrow.