# Not detecting a simple collision?

 0 Hey, folks! I might be crazy, but...for me, the first print statement will print "Normal was: (0,1,0)", but the second print statement then prints nothing. What am I doing wrong, here? I'm not really sure what the heck is up. Why would it print that statement, but then not respond to the conditional? ``````function OnCollisionEnter(collision: Collision) { for (var contact : ContactPoint in collision.contacts) { print("Normal was:" + contact.normal); if(contact.normal == Vector3(0.0, 1.0, 0.0)) { print("Detected collision!"); } } } `````` more ▼ asked Nov 12 '11 at 11:20 AM Catlard 527 ● 48 ● 60 ● 66 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 It's a float issue. The normal is a computed value, so is probably something like `(0, 0.999999, 0.00001)`. The inspector only shows 1 decimal. You can print contact.normal.x*100000 to see more. But, even regular prints often "round to the nearest" on the screen.The official computery way is to check: `if(Mathf.Approximately(contact.normal.x, 0) && Mathf.Approximately(contact.normal.y, 1) ...)`An example of float rounding: 1/3 is 0.3333 with 3's forever. The computer can only store about seven of them. So (1/3)*3 is 0.9999999. Additionally, the computer uses binary, so really stores the 1/2's place, the 1/4ths place, the 1/8ths place ... . One fifth is the non-repeating decimal 0.2 for us, but 1/5th in binary is 1/8 + 1/16 + 1/128 ... and repeats, so has to be rounded to the nearest. more ▼ answered Nov 12 '11 at 03:08 PM Owen Reynolds 12.2k ● 1 ● 7 ● 46 I missed that! You're right: comparing floats equality almost never results true, unless both floats were generated in the same manner (both are constants, for instance). For Vector3 comparison, I would use a margin error: `````` if ((contact.normal-Vector3(0.0, 1.0, 0.0)).magnitude < 0.001){ ... `````` Nov 12 '11 at 04:40 PM aldonaletto You gentlemen are geniuses.I'm trying to make it so that my character can jump through ceilings and land on them as floors--like in most platforming games...I guess I'll start another topic! Thanks again. Nov 12 '11 at 05:24 PM Catlard add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Check if that damned Collapse button in the Console pane isn't pressed: if you click this SOB button by mistake, the Console doesn't show repeated lines, driving us crazy. more ▼ answered Nov 12 '11 at 11:46 AM aldonaletto 42.5k ● 16 ● 43 ● 202 Nope! Not a collapse button issue, I don't think. It's not pressed. And second of all, the second print statement is different from the first--so it wouldn't be collapsed, right?Does this code work for you? I feel like I'm taking crazy pills here. Nov 12 '11 at 11:58 AM Catlard add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x2584
x128
x16
x12

asked: Nov 12 '11 at 11:20 AM

Seen: 702 times

Last Updated: Nov 12 '11 at 05:24 PM