Hello everyone!
I’m having an issue with the raycasthit function. I code using C# by the way!
I’m using the “out RaycastHit” function for Physics.Raycast. The raycasthit Is at a length of 8. However, it goes behind the object. How can I make it go in front? The out raycasthit seems to make the raycasthit go behind, not in front?
Heyo!
Alrighty, so, here is my understanding of what ya told me. Using the Physics.Raycast
function, you are providing a parameter out RaycastHit
and the raycast is going the wrong direction. First of all, to clarify, out RaycastHit
is not quite a function. The out
is a parameter modifier saying that we are passing the RaycastHit we provide by reference. So, the idea is we are shooting out a raycast, and are providing a variable (the RaycastHit) as a place to dump the results of the raycast.
Anyway, to address your question, the RaycastHit doesn’t change anything related to the direction of the raycast. That shouldn’t just make it go backwards. Looking at the documentation for Physics.Raycast, it looks like the 2nd parameter is actually what sets the direction of the Raycast. Now, perhaps you’ve already looked at the documentation and I’m missing something from your code, but without seeing the line of code itself, I feel that’s the best I’ve got. So, something like this is what your code could look like?
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 8, layerMask)) {
Debug.DrawRay(transform.position, transform.forward * hit.distance, Color.red);
}
Hopefully that helps some. If not, let me know and I’ll try again hah.