- Home /
[SOLVED] Raycast only hits one side of the box collider
Hello, I don't know if this question has been asked before, I haven't found it.
Im creating a hook.
Im getting the 3d mouse position using a raycast, then using a raycast from the player position to the 3d mouse position.
for some reason the second raycast only registers a hit in one side of the box collider.
no errors.
Using Unity 2020.2.6
the code:
//some code
RaycastHit mouseHit;
RaycastHit hit;
var mouseRay = main.ScreenPointToRay(Input.mousePosition);
var mouseRaycast = Physics.Raycast(mouseRay, out mouseHit, 999f, mask);
var objectRaycast = Physics.Raycast(transform.position, mouseHit.point, out hit, maxDistanceRaycast, mask);
if (mouseRaycast && objectRaycast){
// Does something
}
Thank you for reading this.
David
did you mean var mouseRaycast = Physics.Raycast(mouseRay , out mouseHit, 999f, mask);
?
i changed the name of the variable to mouseRay just on the question, in the script the name still is ray
Answer by davidbasilefilho · Mar 12, 2021 at 06:14 PM
If anyone have this problem in the future, here's the fix
//some code
RaycastHit mouseHit;
RaycastHit hit;
var mouseRay = main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(mouseRay, out mouseHit);
var objectRaycast = Physics.Raycast(transform.position, (mouseHit.point - transform.position).normalized, out hit, maxDistanceRaycast, mask);
if (objectRaycast){
// Does something
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Mouse Click + Raycast + Colliders 2 Answers
Tryna get the position of the mouse in world space 1 Answer
Raycast executing hundreds of times 2 Answers