Get Texture Coordinates from a Plane when a Mesh or object Intersects/Collides

I'm trying to make a "Worms" type game where an object falls from above and impacts a plane with a texture and causes a portion of the terrain to erase when it contacts a non-transparent part of the texture.

Code I'm currently trying to use can be found here:

alt text

Imagine the surrounding black box is the plane where the image texture ends and the rest is just transparent texture. The red box is the cube mesh that intersects the plane from above. Falling onto it along the same Z axis.

So far I'm able to use Raycasting to get the texture coordinates using mouse input through a script attached to the plane's Update() method:

RaycastHit hit;
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit))
     return hit.textureCoord;

This works fine as a texture eraser using the mouse, but when I try to use a colliding object instead (simple cube), I can't seem to get the texture coordinates (come up zero), even though a collision is being registered. The plane is set to be a trigger, so I'm trying to use OnTriggerStay() to do a raycast from the colliding object's position.

I'm thinking I'm somehow not properly translating the position of the cube in space to planes texture. Though I thought that's what the RayHitcast.textureCoord is supposed to represent.

Please let me know if any clarifications are in order. Thanks!

With some help from mike_mac on #unity3d IRC I was able to solve my issue.

The Raycast was hitting the cube itself instead of the plane. So I created a layer and added the bomb prefab to that layer, then updated the Raycast to check all layers except the weapon layer (in this case was layer 8).

So the Raycast looked like this:

var hitpoint = other.transform.position + Vector3.forward;
if (!Physics.Raycast (hitpoint, Vector3.back * 2.0f, out hitter, 5.0f, ~(1<<8)))

I'll explain the ~(1<<8) as it was the first time I really saw this used. Where ~ is a bitwise NOT and << is a bitwise left shift. So 1<<8 would be just layer 8 and ~(1<<8) is all layers except 8.

Augment the script from the question and this will be complete. :)


Sorry Gravity, I know it has been a long time ago since you posted this, but could you show me the code used to erase the portion of the terrain? Your link is not working. Thanks