Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Unity Raycast results different on two devices

I use the following codes to detect which UI element is the player pointing. It works well in Unity Editor, on one of my Android phone, but failed on a second Android phone.

public static T RaycastOnFirstPointed<T>(EventSystem eventSystem, GraphicRaycaster raycaster)
{
    if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
    {
        if (Input.touches == null || Input.touches.Length == 0) return default;
    }
    var m_PointerEventData = new PointerEventData(eventSystem);
    m_PointerEventData.position = Input.mousePosition;

    var results = new List<RaycastResult>();
    raycaster.Raycast(m_PointerEventData, results); // On the 2nd phone the results.length is 0
    foreach (RaycastResult result in results)
    {
        var component = result.gameObject.GetComponent<T>();
        if (component != null)
        {
            return component;
        }
    }
    return default;
}

This method is called within a IEndDragHandler.OnEndDrag method. I tried to debug it on the 2nd Android phone, and saw the results.length is 0 after Raycast was executed.(it should be > 0 since I ended my drag on several overlapped UI elements, and it is > 0 on the 1st phone, not sure why the 2nd phone is different)

I don't have any clue where should I go from here to find the problem. Please show me some directions, thank you!

More info: Phone 1: MI 8 Lite, OS: MIUI 10.3 Phone 2: MI 9, OS: MIUI 10.2.35

============Update===========

Reason found: When IEndDragHandler.OnEndDrag is triggered, the Input.mousePosition has different value on the two devices. 1st Phone has the touch position from the last frame, while the 2nd phone seems clears that value, and it has a very large wrong value. I'm working on how to solve this in a proper way. Any suggestions are welcomed.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Problem solved. As I updated in the question, this problem is caused by wrong Input.mousePosition value on different devices. So I updated the RaycastOnFirstPointed method as follows:

public static T RaycastOnFirstPointed<T>(EventSystem eventSystem, GraphicRaycaster raycaster, PointerEventData eventData = null)
{
    if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
    {
        if (Input.touches == null || Input.touches.Length == 0) return default;
    }
    var m_PointerEventData = new PointerEventData(eventSystem);
    if (eventData == null)
    {
        m_PointerEventData.position = Input.mousePosition;
    }
    else
    {
        m_PointerEventData.position = eventData.position;
    }
    var results = new List<RaycastResult>();
    raycaster.Raycast(m_PointerEventData, results);
    foreach (RaycastResult result in results)
    {
        var component = result.gameObject.GetComponent<T>();
        if (component != null)
        {
            return component;
        }
    }
    return default;
}

When call this method within OnEndDrag, pass in the PointerEventData that unity provides and use its position.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!