physics.raycast and collider scale - only work on big objects

Hi,
Hello all :wink:

I’ve a problem when I do that :

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 :wink:

1 Like

What’s the original scale of the object? (That is, the scale that isn’t working?)

Also, this:

var direction = transform.TransformDirection(Vector3(0,0,1));

Can simply be written as:

var direction = transform.forward;

The scale of the original cube (wich is not working) is 1,1,1
I just added a simple cube from the gameobject menu

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.)

Hi,

Thanks for your answers, here is the package :

http://ks24521.kimsufi.com/unity/physicraycastproblem.unitypackage

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 :expressionless:

I don’t know if it’s relevant, but I use the FPS creator pack from Dastardly Banana ( http://dastardlybanana.com/Downloads.htm )

Thanks a lot for your help :slight_smile:

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 :wink:

Thanks for your help :slight_smile:

1 Like