Search Unity

Moving objects with Input.mouse in PC works but not with Android

Discussion in 'Android' started by gillemp, Jun 24, 2017.

  1. gillemp

    gillemp

    Joined:
    Nov 23, 2015
    Posts:
    81
    It may sound so weird but it has been even more weird for me trying to know why it happens.

    I've done a code that detects (from the main camera) if I'm holding the mouse above an object and if it is true, save the object in a variable:
    Code (CSharp):
    1. void Update() {
    2.     if (Input.GetMouseButton(0)) {
    3.  
    4.         if (gC.ballBeingMoved == null)
    5.             hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    6.  
    7.         if ((hit != null) && (hit.collider != null)){
    8.  
    9.             if ((gC.ballBeingMoved == null) && (hit.collider.gameObject.tag == "ball")) {
    10.                 gC.ballBeingMoved = hit.collider.gameObject;
    11.                 hit.collider.gameObject.GetComponent<scr_ball>().startMoving();
    12.             }
    13.  
    14.         }
    15.  
    16.     } else {
    17.         gC.ballBeingMoved = null;
    18.     }
    19. }
    In every object that can be moved by the mouse I've put this other code to prepare the moving:
    Code (CSharp):
    1. public void startMoving() {
    2.  
    3.     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4. }
    And in the Update I've put another code to update the position of the object:
    Code (CSharp):
    1. void Update (){
    2.  
    3.     if (GameObject.ReferenceEquals( gC.ballBeingMoved, this.gameObject)) {
    4.  
    5.        Vector3 curPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
    6.  
    7.        print(curPosition);
    8.        transform.position = curPosition;
    9.    }
    10. }
    It works perfectly in PC but when I test it in Android it stops working as how it should. The balls start moving to a coordenates that are not even close to the mouse and even the Unity's console sometimes prints an error that says that an object can not be placed at the given coordenates and that the Input position is { Infinity, -Infinity, 0.000000 }. However if I print the curPosition variable the values that I can see trugh the console are the proper ones.

    I have no idea why it happens but is really pissing me off >.< Can it be a Unity's bug? Or am I missing something there? Thanks for your help.
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,022
    Hi gillemp,

    could you please submit a bug report with your problem?
    Preferably with a minimal project that reproduces the issue.

    Thanks!
     
  3. gillemp

    gillemp

    Joined:
    Nov 23, 2015
    Posts:
    81
    I solved it. It was another part of code that was in conflict with the one that I wrote.