Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with SPHERECAST and HIT.TEXTURECOORD

Discussion in 'Scripting' started by ThiagoSCF, Oct 21, 2013.

  1. ThiagoSCF

    ThiagoSCF

    Joined:
    Oct 24, 2012
    Posts:
    14
    When I cast a regular raycast against an object with mesh collider it outputs a hit.texturecoord just fine, but when I use a spherecast instead it always outputs a Vector2.Zero (0, 0).

    Here is the code I am using:

    Code (csharp):
    1.  
    2. Physics.SphereCast(targetObj.position, rayThickness, targetObj.up, out hit)
    3. Physics.Raycast(targetObj.position, targetObj.up, out hit)
    4.  
    In both cases the hit.point output is the same and all scene conditions are the same. I don't know why the spherecast's output for hit.texturecoord is always Vector2.Zero. :confused:

    Help please!
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yep. It dun work. Barycentrics are also zero, and lightmapCoord seems to be off too.
     
    mdk-117 likes this.
  3. ThiagoSCF

    ThiagoSCF

    Joined:
    Oct 24, 2012
    Posts:
    14
    So, is there any other way to cast a thicker raycast and be able to get the hit.texturecoord?
     
  4. ShuuGames

    ShuuGames

    Joined:
    Jun 25, 2013
    Posts:
    188
    Have you tried to isolate the problem? I made a plane (which has a mesh collider by default) and Physics.SphereCast hit contained textureCoord as expected just like in Raycast. Maybe the collider you hit is not a mesh collider? Check the hit transform name, maybe you're hitting your character collider first?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Thats exactly what I'm doing (with plane and quad primitives) with this code:
    Code (csharp):
    1.     void Update () {
    2.         Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
    3.         RaycastHit hit;
    4.         s = "";
    5.         if ( Physics.SphereCast( ray, 1, out hit, 100 ) ) {
    6.             s += "sphere cast hit at " + hit.point + " tc: " + hit.textureCoord.x + "," + hit.textureCoord.y + "\n";
    7.         }
    8.         if ( Physics.Raycast( ray, out hit, 100 ) ) {
    9.             s += "ray cast hit at " + hit.point + " tc: " + hit.textureCoord.x + "," + hit.textureCoord.y + "\n";
    10.         }
    11.     }
    12.  
    13.     void OnGUI () {
    14.         GUI.Label( new Rect( 0, 0, 500, 500 ), s );
    15.     }
    16.  
    And the results do not match up
    For a plane:
    sphere cast hit at (-0.4, -1.7, 3.0) tc: 0.5,0.6
    ray cast hit at (-0.4, -1.7, 3.0) tc: 0.5356835,0.6708702
    For a Quad:
    sphere cast hit at (2.1, 1.2, 3.0) tc: 0,0
    ray cast hit at (2.1, 1.2, 3.0) tc: 0.9152263,0.7356835

    They clearly hit at the same point, but the tc's don't match
    (note that there is no rounding in my code, and even if there was, 0.915 does not round to zero)
     
  6. ShuuGames

    ShuuGames

    Joined:
    Jun 25, 2013
    Posts:
    188
    Noticed it now too. The spherecast seems to return uv coordinates of some nearby vertex. And in case of a plane, it may even not be a corner of the face, the spherecast hits. Something's really iffy here.
     
  7. DarthDisembowel

    DarthDisembowel

    Joined:
    Jun 11, 2014
    Posts:
    54
    I have this problem too, now, 2 years later. Raycast returns a RaycastHit with correct texturecoord, but switching to Spherecast always returns texturecoord = (0,0).

    Sucks because for my needs the spherecast works much better. I suppose I can use spherecast to detect the hit, then raycast to the hit position to pick up the proper texturecoord, but that's such a waste.