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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mouse Position dont work in Android Tablet

Discussion in 'Scripting' started by ignacionieto, Nov 2, 2016.

  1. ignacionieto

    ignacionieto

    Joined:
    Aug 31, 2014
    Posts:
    6
    Dear all

    I work this code. In the unity interfacit worrks well, but when I uploaded to a Android Tablet the line is not draw in the right position. And everythin goes chaotic. Android just Draw the line elsewhere. Did I made something bad? Any help I will apprecciate.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class dibujaLinea : MonoBehaviour{
    6.     public int click;
    7.     private LineRenderer renderoLinea;
    8.     public void Start(){
    9.        
    10.         // setea el compponente de LineRenderer
    11.         renderoLinea = gameObject.AddComponent<LineRenderer>();
    12.         renderoLinea.enabled = false;
    13.     /*    if (SceneManager.GetActiveScene ().name == "01A" || SceneManager.GetActiveScene ().name == "01B") {
    14.             renderoLinea.SetWidth (0.1f, 0.1f);
    15.         }
    16.         if (SceneManager.GetActiveScene ().name == "03") {
    17.             renderoLinea.SetWidth (1.0f, 1.0f);
    18.         } */
    19.     }
    20.  
    21.     // setea posiciones
    22.     private Vector3 posicionInicial;
    23.     private Vector3 posicionActual;
    24.  
    25.  
    26.     public void Update(){
    27.         if (Input.GetMouseButtonDown (0)) {
    28.            
    29.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    30.             RaycastHit hit;
    31.  
    32.             if (Physics.Raycast (ray, out hit)) {
    33.                 if (click==0 && hit.collider.name == "Dos_silabas" ||click==0 && hit.collider.name == "Tres_silabas" || click==0 && hit.collider.name == "Oreja_Gato"  || click==0 && hit.collider.name == "Oreja_Toro"  || click==0 && hit.collider.name == "Oreja_Zapato" || click==0 && hit.collider.name == "Oreja_Papa" || click==0 && hit.collider.name == "Oreja_Sopa") {
    34.                     click = 1;
    35.                 }
    36.                 else if(click==1 && hit.collider.name == "Dos_silabas" ||click==0 && hit.collider.name == "Tres_silabas" || click==0 && hit.collider.name == "Oreja_Gato"  || click==0 && hit.collider.name == "Oreja_Toro"  || click==0 && hit.collider.name == "Oreja_Zapato" || click==0 && hit.collider.name == "Oreja_Papa" || click==0 && hit.collider.name == "Oreja_Sopa") {
    37.                     click = 0;
    38.                 }
    39.  
    40.                 else if (click== 1 && hit.collider.name != "Dos_silabas" || click == 1 && hit.collider.name == "Tres_silabas") {
    41.                     click=0;
    42.                 }
    43.            
    44.             }
    45.         }
    46.         if (click == 0) {
    47.             posicionInicial = GetCurrentMousePosition ().GetValueOrDefault ();
    48.             renderoLinea.SetPosition (0, posicionInicial);
    49.             renderoLinea.SetVertexCount (1);
    50.             renderoLinea.enabled = true;
    51.         }
    52.  
    53.         if (click == 1) {
    54.  
    55.             posicionActual = GetCurrentMousePosition ().GetValueOrDefault ();
    56.             renderoLinea.SetVertexCount (2);
    57.             renderoLinea.SetPosition (1, posicionActual);
    58.         }
    59.  
    60.         if (click > 1) {
    61.             renderoLinea.enabled = false;
    62.             posicionInicial = Vector3.zero;
    63.             posicionActual = Vector3.zero;
    64.             var posicionSuelta = GetCurrentMousePosition ().GetValueOrDefault ();
    65.             var direccion = posicionSuelta - posicionInicial;
    66.             click = 0;
    67.         }
    68.     }
    69.     private Vector3? GetCurrentMousePosition(){
    70.  
    71.         // dibuja el rayo
    72.         var rayo = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x,Input.mousePosition.y,-1));
    73.         var plano = new Plane(Vector3.forward, Vector3.zero);
    74.  
    75.         float distanciaRayo;
    76.         if (plano.Raycast(rayo, out distanciaRayo))
    77.         {
    78.             return rayo.GetPoint(distanciaRayo);
    79.         }
    80.         return null;
    81.     }
    82. }
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    I'm not really sure how the mouse events are translated to mobile devices, and i always tried to avoid using it, knowing if i'm working for mobile devices.

    For example, Input.mousePosition returns last (or current) position of the touch. Meaning, even if there are no touches, the mousePosition will still return the last position.

    But in this case, there might be an issue with the screen coordinates. In unity docs for mousePosition it says:
    The bottom-left of the screen or window is at (0, 0).

    And for android 0,0 is at the top-left.
     
  3. ignacionieto

    ignacionieto

    Joined:
    Aug 31, 2014
    Posts:
    6
    So Defero

    When you work with mobile devices. What do you suggest? Is there any other method to work better the code?
    thanks in adavance.
     
  4. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200