Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda