Moving objects with Input.mouse in PC works but not with Android
0 down vote favorite 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: void Update() { if (Input.GetMouseButton(0)) {
if (gC.ballBeingMoved == null)
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if ((hit != null) && (hit.collider != null)){
if ((gC.ballBeingMoved == null) && (hit.collider.gameObject.tag == "ball")) {
gC.ballBeingMoved = hit.collider.gameObject;
hit.collider.gameObject.GetComponent<scr_ball>().startMoving();
}
}
} else {
gC.ballBeingMoved = null;
}
}
In every object that can be moved by the mouse I've put this other code to prepare the moving: public void startMoving() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
And in the Update I've put another code to update the position of the object:
void Update (){
if (GameObject.ReferenceEquals( gC.ballBeingMoved, this.gameObject)) {
Vector3 curPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
print(curPosition);
transform.position = curPosition;
}
}
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.
Answer by gillemp · Jun 24, 2017 at 06:38 PM
I solved the problem. It was a conflict between two parts of the code.