RayCastHit.textureCoord giving Vector2.zero, even on mesh colliders

Hey guys,

This one is a bit of a mind bender.

Basically, I am working with models which have been made in 3dsmax, each model has a number of materials. Each material is assigned to certain faces of the model. Each material is idendical in the texture that it uses etc, the only difference is the faces that the material has been set to render on.

This process is because we want to be able to change the texture certain faces only. 3 textures have been made to simulate further damage to the model.

So, ultimately the plan is to change the texture on a certain material, so that it makes a certain set of faces look more damaged.

Here’s a quick breakdown of how it is done via the programming:

We have the basic texture, which is a .psd file
I then created a color map of the texture, mapping out the faces, basically splitting them up by which faces are assigned to which material.

The goal with this is to get the texture coords of the collision, then get the pixel color of the texture coords on the color map, then based on the color of the pixel on the colour map, I can work out which of the materials to alter in order to simulate damage being done.

So, the problem.

So far I have tried two methods:

  1. Doing a RayCast backwards along the normal of the contactPoint.

  2. Doing a RayCast from a forward facing empty object placed directly infront of the striking weapon.

Both of the above are giving me Vector2.zero for the RayCastHit.textureCoord. I cannot work out what I am doing wrong. Both the collider and collidee have mesh colliders, of which both have “convex” set to true.

Here is the code showing 2) during a collision:

		//check if we hit an enemy
		foreach(ContactPoint contact in collision.contacts)
			if(contact.otherCollider.tag == "Enemy")
				//ultimately, we want to know the color of the color map at the texture coords of the collision
				//raycast using the ray casting from the hammer
				ray = new Ray(raycaster.position,raycaster.forward);
				if(Physics.Raycast(ray, out hitinfo, 1f))
					print ("x:"+hitinfo.textureCoord.x+"   y:"+hitinfo.textureCoord.y);	
					print ("nothing hit");	

raycaster is the Transform of the empty object, placed directly infront of the weapon as a child object.

Any help would be much appreciated.

The above returns 0.1,0.5 when by the
looks of things it should be roughly

Ahhhhh. Texture coordinates are UV coordinates with values in the range of 0 to 1, not pixel offsets into the texture. Multiply these values by the texture width and height. Examine the example script for RaycastHit.textureCoord.