Problem with SPHERECAST and HIT.TEXTURECOORD

When I cast a regular raycast against an object with mesh collider it outputs a hit.texturecoord just fine, but when I use a spherecast instead it always outputs a Vector2.Zero (0, 0).

Here is the code I am using:

Physics.SphereCast(targetObj.position, rayThickness, targetObj.up, out hit)
Physics.Raycast(targetObj.position, targetObj.up, out hit)

In both cases the hit.point output is the same and all scene conditions are the same. I don’t know why the spherecast’s output for hit.texturecoord is always Vector2.Zero. :face_with_spiral_eyes:

Help please!

Yep. It dun work. Barycentrics are also zero, and lightmapCoord seems to be off too.

1 Like

So, is there any other way to cast a thicker raycast and be able to get the hit.texturecoord?

Have you tried to isolate the problem? I made a plane (which has a mesh collider by default) and Physics.SphereCast hit contained textureCoord as expected just like in Raycast. Maybe the collider you hit is not a mesh collider? Check the hit transform name, maybe you’re hitting your character collider first?

Thats exactly what I’m doing (with plane and quad primitives) with this code:

	void Update () {
		Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
		RaycastHit hit;
		s = "";
		if ( Physics.SphereCast( ray, 1, out hit, 100 ) ) {
			s += "sphere cast hit at " + hit.point + " tc: " + hit.textureCoord.x + "," + hit.textureCoord.y + "\n";
		}
		if ( Physics.Raycast( ray, out hit, 100 ) ) {
			s += "ray cast hit at " + hit.point + " tc: " + hit.textureCoord.x + "," + hit.textureCoord.y + "\n";
		}
	}

	void OnGUI () {
		GUI.Label( new Rect( 0, 0, 500, 500 ), s );
	}

And the results do not match up
For a plane:
sphere cast hit at (-0.4, -1.7, 3.0) tc: 0.5,0.6
ray cast hit at (-0.4, -1.7, 3.0) tc: 0.5356835,0.6708702
For a Quad:
sphere cast hit at (2.1, 1.2, 3.0) tc: 0,0
ray cast hit at (2.1, 1.2, 3.0) tc: 0.9152263,0.7356835

They clearly hit at the same point, but the tc’s don’t match
(note that there is no rounding in my code, and even if there was, 0.915 does not round to zero)

Noticed it now too. The spherecast seems to return uv coordinates of some nearby vertex. And in case of a plane, it may even not be a corner of the face, the spherecast hits. Something’s really iffy here.

I have this problem too, now, 2 years later. Raycast returns a RaycastHit with correct texturecoord, but switching to Spherecast always returns texturecoord = (0,0).

Sucks because for my needs the spherecast works much better. I suppose I can use spherecast to detect the hit, then raycast to the hit position to pick up the proper texturecoord, but that’s such a waste.