var hit : RaycastHit;
var layerMask = 1<<10;
var direction = transform.TransformDirection(Vector3(0,0,1));
if(Physics.Raycast(transform.position, direction, hit, itemRange, layerMask)){
Debug.Log("layer : " + hit.transform.name);
}
If the ray hit is a simple cube, it return nothing
If I just rescale the cube to a greater scale (3x the original) it return the correct data
Do you have any idea how to fix it?
Thanks in advance
There should be no problem raycasting against a box collider with those dimensions. If you’re finding this isn’t working, you might consider uploading a Unity package demonstrating the behavior so others can check it out. (Or, if you’re sure the behavior is incorrect, you could file a bug report. My guess would be though that it’s not a bug, and that the problem is elsewhere.)
The expected behavior is when you point the cube with your crosshair at the right distance (10 in this case, you maybe need to move the player a little) the debug log send "layer : " + the name of the gameobject.
The green one is working (scale 5,5,5 not 3,3,3 like I was thinking), the red one is not (scale 1,1,1).
The script is on the player object and is named “usableItems.js”
I really don’t understand, if I use this script on a totaly new project, it work, even on a cube of scale 1,1,1
I took a quick look, and it’s not immediately apparent to me why the red cube isn’t responding to raycasts.
What I’d recommend doing is creating a minimal example project that demonstrates the problem, for example a scene with nothing in it but a cube with scale (1, 1, 1), and a fixed raycast that should intersect the cube. If that ends up working correctly, you may need to add things progressively until it stops working.
If you can only get it not to work in the context of your current project, you could submit that project along with a bug report to UT. If on the other hand you can create a more minimal example, I’d send that instead.
I know that’s not particularly helpful, but I don’t have any other suggestions at the moment. (I’ve certainly never seen that behavior before, and if there’s an obvious explanation for it, I must be overlooking it.)
I got it…
It was a positioning problem, the ray was fired from the player game object, but in order to hit the objects pointed by the crosshair it needed to be in the weapon object (a children of player)
My fault