Search Unity

Strange Bug Depends on Where Camera's Pointing

Discussion in 'Editor & General Support' started by John-B, Oct 17, 2014.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    I have a cannon that shoots a ball, very simple. It applies a one-time impulse to launch the ball. The ball has a script attached that plays a sound on collision enter and records the location of the collision. This works as expected MOST of the time. But under very specific conditions, the ball's scale gets screwed up on collision, one axis getting very small and one axis getting very big, resulting in an elongated pancake. Nowhere in any script is this or any other localScale set, so this seems very odd to me.

    Now the conditions: This only happens on iPad, never in the editor. It only happens with certain cannon locations/rotations. AND it only happens if the ball's collision with the wall or floor is out of view of the camera. If I point the camera at where the ball lands, it NEVER happens. The only difference being the rotation of the camera. The camera has to be pointing away from the impact point, then when the ball bounces back into view, it's a pancake.

    I've discovered that if I comment out the OnCollisionEnter function attached to the ball, that fixes the problem. Which makes no sense. Here's that function:

    Code (JavaScript):
    1. function OnCollisionEnter (col: Collision) {
    2.      if (col.relativeVelocity.magnitude > 0.1 && (col.transform.name.Contains("Floor") || col.transform.name.Contains("Wall"))) {
    3.          myHitSnd.volume = col.relativeVelocity.magnitude * 0.3;
    4.          myHitSnd.Play();
    5.          
    6.          if (col.relativeVelocity.magnitude > 2.0) {
    7.                lastCollisionPosOld = lastCollisionPos;
    8.                lastCollisionPos = transform.position;
    9.                lastCollisionTime = Time.realtimeSinceStartup;
    10.          }
    11.      }
    12. }
    One other thing that might be relevant, the ball is imported with unequal scale values, a quirk of Strata. The ball's scale is 7, 7, 0.35 (it's round), and it changes if I change its parent. Still, it shouldn't matter since the scale is never changed (by my code).

    My only solution has been to reset the localScale of the ball on collision.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Judging from your first paragraph, it sounds like a scaling issue. If the object being rotated scale is not 1,1,1 ( or at least uniform on 3 axis, it can warp its contents.
     
  3. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    What can I do about that? That's the way objects created in Strata import (exported as Collada), often with strange scale values (and unnecessary parents). I've tried rearranging parent/child relationships, which sometimes changes the scale, but never in a good way. I don't know of any other way to change an object's scale in Unity without actually changing its size.

    FWIW, I'm not rotating the ball, or changing its position. It's non-kinematic. I'm just applying a force.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Not really sure. I don't use or know Strata. Also, I only work in 2D. But, I am always mindful of parents scale and their children.

    One thing I do do, if I know an object may be moved, scaled or rotated, I generally create a tree like....

    The Object - root generally scaled to 1.
    Sub Transform - first child of root. This I generally use to move scale, rotate etc.
    All contents within sub transform.
     
    Last edited: Oct 19, 2014
  5. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    I already tried making the ball a child of an empty object with a scale of 1, 1, 1, but that didn't fix the problem. I think once an object is imported with an odd scale, there's nothing that can be done in Unity.
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Why? Object.localScale ='s...

    That something that can be adjusted at anytime.
     
  7. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    If the scale of the imported ball is 7, 7, 0.35, I can certainly change those values. But I can do nothing to make all three equal and still keep the ball round, AFAIK. For example, if I set the scale of the ball to 7, 7, 7, it will be an oval, not a round ball. Makes no sense to me, but that's the way it is.
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hm.
    So is the ball in a parent?

    Can you place this object in a new parent scaled at 1?
     
  9. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    This is just crazy. I thought I had it fixed, but no.

    When I display the ball's scale on launch in the editor, it shows 7, 7, 0.35, as it shows in the Inspector. But when I show the ball's scale on launch on the iPad, it shows 50.4, 50.4, 50.4. After a collision, both in the editor and on iPad, it shows 7, 7, 0.35. ONLY if the collision is not in view of the camera. Setting it back to it's start value of 50.4, 50.4, 50.4 after a collision does not work, it still looks flat.

    Yes, the ball is a parent of the cannon, actually a child of a child. I tried making a new ball, a Unity primitive, but as soon as I make it a child, it's scale gets screwed up, two values the same, one different. I also tried making a new parent for the ball scaled at 1, 1, 1, but as soon as the ball becomes a child, it's scale gets screwed up.

    One other weird thing, on iPad, the parent of the ball changes when it's shot, which I'm guessing is the root of the problem. No way it should do that. When I show the parent when running in the Inspector, it shows the ball to be a child of a child of the cannon. It never changes. But on iPad, on app launch the ball's parent is the cannon. After it's shot, it shows it to be a child of a child, as it should. How can the parent-child relationship be different in the editor and on iPad???? And why would this change in the parent-child relationship only matter when it happens out of view?
     
    Last edited: Oct 23, 2014
  10. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    The solution (I hope) was to give up on trying to compensate for all the scaling weirdness, and just use two balls. The ball has to be a child of a child while the cannon is being manipulated (some interconnected moving parts), but not when its shot. So I made a 2nd dummy ball (no collider or rigidbody) just for show. When the cannon is shot, I hide the dummy ball and show/shoot the actual ball, which is NOT a child of a child, and has equal scale values. A bit convoluted, but much less so than the alternative.
     
  11. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Cool.
    It seems wierd. How could a sphere, not scaled to one, look. Like it is scaled to one?? Anyhow, hack solutions are often the only solutions.

    #FreeRicky
     
  12. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I would try to determine what aspect of the OnCollisionEnter function is causing or contributing to the problem.

    For instance, you might comment out the body of the function and see what happens.

    Or, you might comment out the body of the first if statement.

    Or, you might comment out the body of the second if statement.

    Perhaps you can isolate the issue to one line of code?