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
  4. Dismiss Notice

Physics Behaving Differently on Different Devices

Discussion in 'Scripting' started by vabster, Jan 14, 2016.

  1. vabster

    vabster

    Joined:
    May 12, 2013
    Posts:
    14
    Hello!

    I'm creating a mobile game where you can pick up and throw game objects. The velocity of the throw depends on how fast the user is moving their finger before letting go. I have a script that does this well, but unfortunately the velocity at which the object is thrown is changes based on the device the player is using. For example the velocity of the thrown object is faster on an iPad Air 2 than on an iPhone 6, and the velocity of the throw is slower on an iPad 2.

    I've narrowed down the location of where the problem lies, but I can't seem to figure out how to fix it. Below is the relevant code

    Code (CSharp):
    1.     Dictionary<int, DragData> draggedObjects;
    2.     struct DragData
    3.     {
    4.         public Transform draggedTransform;
    5.         public Rigidbody2D draggedRigidbody;
    6.         public HingeJoint2D draggedJoint;
    7.      
    8.     }
    9.  
    10.     void init()
    11.     {
    12.         draggedObjects = new Dictionary<int, DragData>();
    13.     }
    14.  
    15.  
    16.  
    17.     void FixedUpdate()
    18.     {
    19.        ...
    20.         DragData data = draggedObjects[entry];
    21.  
    22.         // When object is dragged
    23.         if (touch.phase == TouchPhase.Moved ||
    24.             touch.phase == TouchPhase.Stationary)
    25.         {
    26.             // Gets the position of the user touch
    27.             Vector3 touchCoords = touch.position;
    28.             // Gets the position of the dragged object
    29.             Vector3 objectCoords = GetComponent<Camera> ().WorldToScreenPoint(data.draggedTransform.position );          
    30.             float distance = Vector2.Distance(objectCoords, touchCoords);
    31.             // Drag Damping is a public float
    32.             Vector2 objectVelocity = (touchCoords - objectCoords).normalized * (distance / (dragDamping));
    33.          
    34.             data.draggedJoint.GetComponent<Rigidbody2D>().velocity = objectVelocity;
    35.                  
    36.         }
    37.     }

    The velocity set in the final line of code seems to be higher on more advanced devices, but I'm not sure which line of code is causing this or how to fix it. Any help would be greatly appreciated.
     
    Last edited: Jan 14, 2016
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    My first guess would be the faster devices have higher resolution screens and so you have a higher distance between the start and end. Try using a normalised distance.
     
    Kiwasi, vabster and hippocoder like this.
  3. vabster

    vabster

    Joined:
    May 12, 2013
    Posts:
    14
    Thanks so much for the reply Karl, I thought that the screen resolution may have been the culprit. Sorry if this is a dumb question, but how would I go about normalizing the distance? And once again, thanks for the help!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Try dividing the distance by the screen dimensions.
     
    vabster and karl_jones like this.
  5. vabster

    vabster

    Joined:
    May 12, 2013
    Posts:
    14
    Thanks for the help guys, I believe it's now working just how I wanted.
     
    karl_jones likes this.