Search Unity

Bizzare logic?

Discussion in 'Scripting' started by Flynn, Apr 27, 2012.

  1. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Hi! I have this code:


    Code (csharp):
    1. private void HandlePositioning()
    2. {
    3.        
    4.         if (Selection.Server.Selected.Count == 0)
    5.         {
    6.             transform.localScale = new Vector3(0, 0, 0);
    7.                 Debug.Log("ZERO");
    8.         }
    9.         else
    10.         {
    11.                 Debug.Log("NOT ZERO");
    12.         }
    13.  
    14.         HandlePartScales();
    15.     }
    This gets called every update. When "transform.localScale = new Vector3(0, 0, 0);" is not inside the first if statement, the if statement NEVER gets called. However, if I ever add the "transform.localScale = new Vector3(0, 0, 0);" line to the if statement, I start getting ZERO debugs! I am rather confused by this.

    Basically, the if statement is executing when it should not, but only if the transform statement is inside of it. If the transform statement isn't there, the if statement only gets called when it should. NOTE: This isn't the full code. The full code is somewhat complex, there are a lot of interconnecting parts. The only situation in which Selection.Server.Selected.Count can become zero (at least, with the input I am giving to the code to cause this issue_ is if the transform scale is set to zero. But the transform scale should only ever go to zero if the count is zero! It's very confusing. Anyone have any ideas?
     
  2. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    By "NEVER gets called" I assume you mean "NOT ZERO" is printed, and when you add the localScale line "ZERO" gets printed?
     
  3. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    I've done more testing, and come across what appears to be a Unity bug!

    As it turns out, if a transform ever goes to the scale (0, 0, 0) it becomes untouchable by raycasts.

    I will do some further tests to be positive.
     
  4. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Is that really a bug? I would assume an object of scale zero would mean it essentially doesn't exist, no?
     
  5. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Yes, "NOT ZERO" was always printed. But when adding the line, localScale gets printed.

    I think I know what was happening.


    In my scene I have a cube. It starts out at the scale 0, 0, 0 (form this code) in the beginning of the game. Later on, after a user action, it becomes a normal scale. At this point, the user is capable of clicking. If the user clicks on an area that is not the cube, the cube goes back to size zero. If the user clicks directly on the cube, the cube moves around.

    Or so I thought.

    I use ray casting to detect if the user clicks on the cube. Because of that line of code ( the transform.localScale one) the cube starts at size zero until later. When the cube finally becomes normal sized, the raycasting becomes "corrupted" and therefore, clicking directly on it doesn't activate the raycast! I THOUGHT that the cube was going back to 0, 0, 0 as a result of being clicked on. It turns out it simply was never clicked on if that makes sense at all
     
  6. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    The problem is that if the object ever becomes 0, 0, 0 it stops working for good. IE, it will not respond to raycasts even if you bring it back to normal scale
     
  7. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Well that's interesting... so raycasts work fine and dandy, but if you scale it to zero and back to normal, it won't work anymore? Maybe that's a bug after all...
     
  8. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    That is very strange. But could you get away with scaling it to 0.00001f instead? I doubt a raycast could hit that easily.
     
  9. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Okay! I've finished testing. I have figured out exactly what the bug is, and confirmed it IS a bug.

    I set up a test scene containing one camera looking directly at a cube.

    This was no ordinary cube, the glitch only works with mesh colliders. The cube thus was using a MESH collider, NOT a cube collider.

    The camera has this script attached:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RaycastTest : MonoBehaviour
    5. {
    6.     void Update ()
    7.     {
    8.         RaycastHit hit = new RaycastHit();  
    9.         float distance = 500;
    10.         if (Physics.Raycast(transform.position, Vector3.forward, out hit, distance))
    11.         {
    12.             distance = hit.distance;
    13.         }
    14.         Debug.DrawRay(transform.position, Vector3.forward * distance, Color.green);
    15.     }
    16. }
    Step one: I ran the scene. The raycast worked fine on the object, it collided at the correct point.
    Step two: I set one of the scale axes to 0. Immediately the ray cast started moving through the object.
    Step three: I set the other axes to zero as well. No change. (in tests where I skipped this step, the bug did not occur)
    Step four: I reset all three of the axes to their original scale (3.1) However, the raycast still continued to move through the object!
    The object is now apparently impervious to any form of raycast!
    Step five: I created a sphere (while the scene was still running)
    Step six: I moved the sphere directly above the cube, then added a rigidbody.
    The sphere proceded to fall DIRECTLY through the cube!

    Bug: When an object with a mesh collider has *ALL THREE, only one will not work permanently* axes of scale set to zero, the mesh collider ceases to function. Assuming all three axes were set to zero, the bug is permanent for this object. It will not continue to function if the scale is set back to a valid scale.
     
    Last edited: Apr 27, 2012
  10. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    @Killer that'll have to do, thank you :) I hope this bug gets fixed. I wonder if it's a bug in the underlying physics engine, or just Unity.

    I tried the scale Mathf.Epsilon as well as Vector3.zero. Mathf.Epsilon as well causes the issue. (However numbers like 0.00001f do not, only numbers in the range of scale of Epsilon do apparently)
     
  11. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Wow, that cube being "no ordinary cube" and becoming impervious to any form of rays... sounds like supercube!
     
  12. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    hahaha yes, super cube... Mr. cube... Growing up, he was always a strange one, and outsider at school. Kids made fun of him for his meshy, non-cube collider... As a young adult, Mr. cube was exposed to extraordinary SCALEO-rays... Normally, this doesn't effect cubes as you know... But something strange happened. Something clicked! His mesh collider reacted to the scalorays, and he became... SUPER CUBE, the master of ghost powers
     
  13. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    Anyway, all bugs aside, do you need to scale it to zero? Why not just disable/enable it when needed?
     
  14. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Slightly simpler code wise to scale it to zero, it all ready has scale-changing code in it which reacts to similar inputs to what the scale-to-zero does. Just made sense to make it infinitely small than go to the trouble of making all the renderers invisible and disabling the colliders, or, the alternative, adding code in the ray casting section to check if it is enabled. One line of code does it all, versus at least ten-twenty, particularly tedius lines of code in any other method
     
  15. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    "I see!!!" said the blind man to the deaf man. I wonder if the collider's "enabled" flag is still set after you scale it to zero. You should submit a bug report, it's in the unity menu.
     
  16. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Ahh thank you.. I was wondering how to submit a bug lol

    Yes, it appears that the enabled flag is still set to true, even after it is scaled to zero