Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Collider.radius question

Discussion in 'Scripting' started by Eric5h5, Oct 6, 2006.

  1. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    So, I have a bunch of objects with sphere colliders, and they're all scaled differently. However, I want the sphere colliders to be the same size when the scene is run, so I have this in a Start function for each object:

    collider.radius = 90 / transform.localScale.x;

    This appears to work fine, judging from the scene view. However, OnTriggerEnter still only works as if it were using the original radius. But here's the weird thing: OnTriggerExit does work with the new radius. Wha...? Is this some weirdness or am I missing something?

    --Eric
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,271
    This may not be it, but try setting the size of the bounding box, also... Collider.bounds.extents.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I tried extents, size, etc. to no avail, but thanks! I think that only applies to box colliders and not sphere colliders anyway. The strangeness of Exit working but Enter not working makes me think it's a bug, so I guess I'll report it unless someone knows better. (Also, switching to a capsule collider does the same thing.)

    --Eric
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    colliders.bounds is ready only and always automatically update when changing the collider.

    If you can produce a small test case, please send it in.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I've narrowed the problem down. Seems it's the result of doing a Physics.Raycast, for some reason. This code results in the collider.radius not working:

    Code (csharp):
    1.  
    2. var hit : RaycastHit;
    3. if (Physics.Raycast (transform.position, Vector3.down, hit, Mathf.Infinity, 1<<8) ) {
    4.     transform.position += Vector3.down * (hit.distance-3);
    5. }
    6.  
    However, if I use this, which moves the object the same distance in this case:

    Code (csharp):
    1.  
    2. transform.position += Vector3.down * 28.3;
    3.  
    Then the collider.radius does work. The only difference is not using raycasting. But that's not all! If the collider.radius is large, and then set smaller (either manually or by scripting), then I can do Physics.Raycast and the collider.radius works fine. It's only going from small > bigger that somehow Physics.Raycast messes it up. But that's still not all! If I take the raycast stuff out entirely and put it in a completely different script attached to a completely different object, the collider radius on the first object still doesn't work right....

    I'm sending an example project that shows this, but if there's something stupid about what I'm doing with the raycasting, then ignore it and hopefully tell me the error of my ways. :)

    --Eric