Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why CaspsuleCast is buggy

Discussion in 'Scripting' started by RoXCoder, Mar 30, 2012.

  1. RoXCoder

    RoXCoder

    Joined:
    Mar 3, 2012
    Posts:
    98
    Hi,

    It's been literally 5 full days that im working with my character collision code using CapsuleCast, and everytime i switch between the 2 codes below, i get a totally different behavior.

    Code (csharp):
    1. if (Physics.CapsuleCast(new Vector3(g_position.x, g_position.y + 0.0f, g_position.z), new Vector3(g_position.x, g_position.y + 2.0f, g_position.z), 0.5f, new Vector3(0, fDist, 0).normalized, out rayHit, Mathf.Infinity))
    Code (csharp):
    1. if (Physics.CapsuleCast(new Vector3(g_position.x, g_position.y + 2.0f, g_position.z), new Vector3(g_position.x, g_position.y + 0.0f, g_position.z), 0.5f, new Vector3(0, fDist, 0).normalized, out rayHit, Mathf.Infinity))
    Notice the p1 and p2 parameters.
    The 2 codes should behave and return the samething, but they dont.
    The first works perfect for me while the second return weird values.
    Why?
     
  2. Aniani

    Aniani

    Joined:
    Mar 17, 2011
    Posts:
    31
    Oh, I found the exact same behaviour a while ago! It seems that the first argument must be the bottom of the capsule and the second the top, otherwise it's all wrong. I wonder if it's something to do with the way it's 'drawn out', maybe the normals of the capsule become inverted or something? I don't know but it's definitely weird :confused:

    Also is there a reason why you have a + 0.0f ? You might as well just write g_position.y
     
  3. RoXCoder

    RoXCoder

    Joined:
    Mar 3, 2012
    Posts:
    98
    Thanks for the info.
    The 0.0f was just to bold out that i swapped the p1,p2.
     
  4. Matt-Downey

    Matt-Downey

    Joined:
    Apr 22, 2012
    Posts:
    11
    Thank you so much. This has helped me tremendously. Usually I add before I subtract and do actions from top to bottom, so I actually spent two days trying the same thing over and over again (always noticing that only SphereCast worked, but never CapsuleCast). I was under the impression this was a driver problem with Nvidia's Physics for my laptop, since my laptop is often buggy as far as my 555M graphics card goes.

    The two days weren't really spent in vain, though, because I actually optimized my spherecast to be able to run without a reduced radius (because if the cast starts out inside of an object, SphereCast/CapsuleCast is not called) by buffering the player away from the wall's position and the floor's position.

    This made my day!
     
    Last edited: Jun 17, 2012
  5. DanJC

    DanJC

    Joined:
    Feb 18, 2013
    Posts:
    15
    Thanks RoxCoder and Aniani, I would have been messing with this for days.

    Nearly 2 years since the original post and this important note still isn't in the docs.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,616
    Well, did anyone bug report it?