x


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: http://www.pasteit4me.com/2277018

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!

more ▼

asked Feb 15 '11 at 05:04 PM

GravitySpec gravatar image

GravitySpec
26 1 1 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

Enjoy!

more ▼

answered Feb 18 '11 at 02:49 AM

GravitySpec gravatar image

GravitySpec
26 1 1 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2584
x2279
x1403
x266
x9

asked: Feb 15 '11 at 05:04 PM

Seen: 1763 times

Last Updated: Feb 18 '11 at 02:05 AM