x


OnMouseDown() cube face?

OnMouseDown() will tell me when an object is clicked. Is there a way to know which face of a cube was touched?

more ▼

asked Nov 27 '10 at 02:35 AM

OmegaVemon gravatar image

OmegaVemon
177 28 31 40

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

1 answer: sort voted first

You'll need to cast a ray from the mouse position into the scene, and examine the RaycastHit result to check which triangle was hit. There are two triangles on each side of a cube, so you'll need to check for both triangles for each side.

Eg:

function OnMouseDown() {
    var hit : RaycastHit;
    if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
    {
        print("Hit triangle index: "+hit.triangleIndex);
    }
}

You'll need to work out which indices relate to which sides, and group them accordingly in to "if" statements, like this:

        if (hit.triangleIndex == 0 || hit.triangleIndex == 1) {
            print ("You hit the top!");
        }

0 and 1 might not be the top, I haven't tested this myself - this is just for illustration!

Good luck!

more ▼

answered Nov 27 '10 at 08:32 AM

duck gravatar image

duck ♦♦
41k 92 148 415

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

x322
x288
x120
x47

asked: Nov 27 '10 at 02:35 AM

Seen: 1313 times

Last Updated: Nov 27 '10 at 02:35 AM