Search code examples
c#androidunity-game-enginetouchscreen-resolution

Drag with speed in Unity game not equal depending on resolution


I'm doing a game in Unity, it is a space shooter and I had made a script for moving my spaceship. This game is developed for Android devices, and I'm moving the ship with the Touch. It is something like dragging the ship, to the position you want. Also one must note, that I'm not considering if you touch in the center of the ship in order to drag it. However I'm having problems, depending on the device where I'm working, the ship is not following correctly the drag, for instance if I'm on a tablet, if I move with the finger, let's say 3 inches, my ship is only moving one. However, on my mobile device it is working fine.

What I'm doing wrong? I attach you the code for the movement:

void MovePlayer (Vector3 movement)
{
    GetComponent<Rigidbody> ().velocity = movement;
    GetComponent<Rigidbody> ().position = new Vector3 
        (Mathf.Clamp (GetComponent<Rigidbody> ().position.x, boundary.xMin, boundary.xMax), 
         0.0f, 
         Mathf.Clamp (GetComponent<Rigidbody> ().position.z, boundary.zMin, boundary.zMax));
    GetComponent<Rigidbody> ().rotation = Quaternion.Euler 
        (0.0f, 
         0.0f, 
         GetComponent<Rigidbody> ().velocity.x * -tilt);
}

void FixedUpdate ()
{
    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {

        // Get movement of the finger since last frame
        Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;

        float moveHorizontal = touchDeltaPosition.x;
        float moveVertical = touchDeltaPosition.y;

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical) * speedMouse;
        MovePlayer (movement);
    }
}

Lot of thanks in advance and best regards


Solution

  • The problem here is you are using a speedMouse variable. Speed should have absolutely nothing to do with this, and would be the exact reason for your problem. You should be moving objects based solely on position of touch/mouse. not with speed. because speed is the only thing that will differ your output from cross resolutions.

    Think about it this way, if your speed is 10 pixels/second you will move a lot slower on a denser screen, however if you are only updating position based on location on the screen, speeds will never differ.