Search Unity

SphereCasting... Explain to me please

Discussion in 'Scripting' started by Subliminalman, Sep 16, 2012.

  1. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    So I'm currently trying to make a 2d fan object that will blow the player about. I found this nifty little Casting function called SphereCasting that sounded pretty much perfect. Thing is it is only returning true on objects that have a sphere collider. This is really confusing to me seeing that the documentation says that it will return true for any collider that is not a trigger. What is going on?

    Here's my testing code, if someone can explain to me what I am doing wrong that would be great!

    Code (csharp):
    1.  
    2. if(Physics.SphereCast(transform.position, 10, petTransform.position - transform.position, out hit))
    3. {          
    4.     Debug.Log("" + hit.collider.name);
    5. }
    6.  
    Thank you in advanced
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I just ran my own SphereCast and it picked up cubes, capsules, and spheres.
     
  3. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    Is it pretty much the same as my code above?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    yep.

    question... is there a sphere collider on 'this'? Maybe you keep picking up that collider, because SphereCast grabs the nearest/first collider it reaches.
     
  5. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    There is no collider on the object with the spherecast script
     
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    SphereCast only returns the first thing that is hit; SphereCastAll will return everything that is hit. Maybe that's what you're missing?

    Also, are you using Unity 3.5 or 4? Someone else posted a bug about sphere colliders in Unity 4 today, so maybe something's wonky with them.