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

[SOLVED] Physics.Raycast is inconsistent?

Discussion in 'Physics' started by FoxAdventures, Jan 16, 2022.

  1. FoxAdventures

    FoxAdventures

    Joined:
    Jan 31, 2018
    Posts:
    71
    The main idea of the prototype is that the player drag and drops the UI button and a cube spawns where the player releases their finger. But it seems to not work for some parts of the screen.


    Code (CSharp):
    1.     public void OnDrop(PointerEventData eventData)
    2.     {
    3.         Touch touch = Touch.activeFingers[0].currentTouch;
    4.  
    5.         RaycastHit hit = new RaycastHit();
    6.         Ray ray = Camera.main.ScreenPointToRay(touch.screenPosition);
    7.  
    8.         if (Physics.Raycast(ray, out hit))
    9.         {
    10.             print("obj: " + hit.collider.gameObject.name);
    11.             cube.SetActive(true);
    12.             cube.transform.position = new Vector3(0, hit.point.y, hit.point.z);
    13.  
    14.         }
    15.  
    16.        
    17.  
    18.     }
    The scene and game views are attached below. The "background" is just a stretched cube that I added so that the raycast hits something. But the raycast seems to only work about 50% of the screen, like points scattered randomly throughout.
     

    Attached Files:

  2. TheHowYou

    TheHowYou

    Joined:
    Nov 18, 2021
    Posts:
    3
    I'm by no means amazing at any of this, but I thought I might be able to help a little.

    I would suspect that the issue isn't your Raycast but rather the way you're getting your touch input. I have no experience with touch input, btw. So I'm not much help there, but are you sure the input info is always stored in the [0] array? I feel like there are better ways to be fetching this input.

    My other suggestion would be to use a Debug.DrawRay to help with visually debugging what is going on.
    https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

    Hope that helps
     
  3. FoxAdventures

    FoxAdventures

    Joined:
    Jan 31, 2018
    Posts:
    71
    Hello, thanks for answering.

    Yes it is valid to use activeFingers[0].currentTouch . it is the new input system syntax. I will try the DrawRay function, to see if that helps.
     
  4. FoxAdventures

    FoxAdventures

    Joined:
    Jan 31, 2018
    Posts:
    71
    This is what it looks like when the raycast hit the background object. The raycast line seems to be completely wrong.
     

    Attached Files:

  5. FoxAdventures

    FoxAdventures

    Joined:
    Jan 31, 2018
    Posts:
    71
    Update: it seems the problem was in two places: first, it shouldn't have been "OnDrop", i do not know why, maybe not everything can get registered fast enough in that method. the other problem lies in the very last line of my first message, when I tried to set the cube's transform to something other than the hit point. For anyone who might have a similar problem to mine, here is what it looks like at last:

    Code (CSharp):
    1. public void OnEndDrag(PointerEventData eventData)
    2.     {
    3.         transform.position = Vector3.zero;
    4.  
    5.         if (Touch.activeFingers.Count > 0)
    6.         {
    7.             print("hh");
    8.  
    9.          
    10.             Touch touch = Touch.activeFingers[0].currentTouch;
    11.  
    12.             RaycastHit hit = new RaycastHit();
    13.             Ray ray = Camera.main.ScreenPointToRay(touch.screenPosition);
    14.  
    15.             if (Physics.Raycast(ray, out hit, float.PositiveInfinity))
    16.             {
    17.                 print("ok");
    18.  
    19.                 cube.SetActive(true);
    20.                 cube.transform.position = hit.point;
    21.  
    22.             }
    23.  
    24.         }
    25.  
    26.     }
    27.  
    28. private void Update()
    29.     {
    30.         Debug.DrawRay(Camera.main.transform.position, hitRef, Color.red);
    31.         cube.transform.position = new Vector3(0, cube.transform.position.y, cube.transform.position.z);
    32.     }
    The problem is solved so, the moderators can safely lock the thread.
     
    tmanallen and Jakuiz like this.