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

Similar Physics.Raycast behaving differently depending on hitInfo Parameter

Discussion in 'Scripting' started by AllTheGoodNamesWereTaken, Apr 28, 2017.

  1. AllTheGoodNamesWereTaken

    AllTheGoodNamesWereTaken

    Joined:
    Sep 24, 2015
    Posts:
    14
    Code (CSharp):
    1. Physics.Raycast(transform.position, Vector3.down, out hit, .55f)
    Seems to work as intended with both a default cube and my mesh collider but,
    Code (CSharp):
    1. Physics.Raycast(transform.position, Vector3.down,  .55f)
    works as intended with a default cube but not my mesh collider. They should both return (bool) True when the ray intersects any collider so why would including the hitInfo parameter change the way it functions?
     
  2. AllTheGoodNamesWereTaken

    AllTheGoodNamesWereTaken

    Joined:
    Sep 24, 2015
    Posts:
    14
    Let me elaborate in hopes some one can shine some insight on this problem:

    This is my test area (Game view & ground mesh collider). GameView&Level1MeshCollider.png
    Here are my objects.
    Objects.png

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestRay : MonoBehaviour{
    4.  
    5.     private RaycastHit hit;
    6.  
    7.     void Update(){
    8.         if (Physics.Raycast(transform.position, Vector3.down, 0.1f)) Debug.Log ("Is ray intersecting with a Collider: " + Physics.Raycast(transform.position, Vector3.down, 0.1f));
    9.     }
    10. }
    Since the raycast (from the top cube) starts at transform.position (inside of the cube), a length of .1 should not be able to collide with anything outside of the cube since the cube is a size of 1. As you can see here though a collision is detected:
    NoOutHit.png
    (Bottom cube Y-axis locked) As tested above the ray is not detecting the cube below it even when it falls directly on top of it. It is how ever detecting the ground when is is raised up. It seems to be detecting a raycast hit though the cube and on the ground way below it.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestRay : MonoBehaviour{
    4.  
    5.     private RaycastHit hit;
    6.  
    7.     void Update(){
    8.         if (Physics.Raycast(transform.position, Vector3.down, out hit, 0.1f)) Debug.Log ("Is ray intersecting with a Collider: " + Physics.Raycast(transform.position, Vector3.down, out hit, 0.1f));
    9.     }
    10. }
    For some odd reason adding the hitInfo parameter makes it seem to work as intended.
    OutHit.png
    No collision is detected on top of the cube or the ground.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestRay : MonoBehaviour{
    4.  
    5.     private RaycastHit hit;
    6.  
    7.     void Update(){
    8.         if (Physics.Raycast(transform.position, Vector3.down, 1f)) Debug.Log ("Is ray intersecting with a Collider: " + Physics.Raycast(transform.position, Vector3.down, 1f));
    9.     }
    10. }
    (Top left) Ground and cube start at default position, top cube Y-axis locked. (Top right) With no hitInfo parameter and increasing the ray length to 1, this is where the hit detection first occurs after raising both the bottom cube and the ground :
    With&WithoutOutHit.png
    (Bottom left) Again default positions and top cube Y-axis locked. (Bottom right) Ray length at 1 and hitInfo parameter included. After raising both the bottom cube and ground they detect a hit at the exact place you would expect them to.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestRay : MonoBehaviour{
    4.  
    5.     private RaycastHit hit;
    6.  
    7.     void Update(){
    8.         if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f)) Debug.Log ("Is ray intersecting with a Collider: " + Physics.Raycast(transform.position, Vector3.down, out hit, 1f));
    9.     }
    10. }
    Including or excluding the hitInfo parameter should not change the way the raycast works at all because it is only including additional hit information. There's no reason I know of that should make a raycast detect my ground from farther away than it should. I am including it in my code just to make it work like it should but then I have to constantly deal with the: Assets/TestRay.cs(5,21): warning CS0414: The private field 'TestRay.hit' is assigned but its value never used.

    Any thoughts or insights are welcome.