Drag to add power, release to throw at opposite angle (RigidBody2D), strange beahviour.

I’m a total beginner and I’m making a simple 2D game, you have ball which you throw to collect pickups. I managed, following tutorials and searching on the web, to script the controls as I wanted them, when the mouse is clicked the force to apply is calculated by the movement on axis X and Y and on mouse button release the force is applied and the gravity turned on:

    if (Input.GetMouseButton(0)) mouseDown = true;
    if (!Input.GetMouseButton(0))  mouseDown = false;         

    if (Input.GetAxis("Mouse X") != 0 && mouseDown)
    {            
        xForce -= Input.GetAxis("Mouse X") * 50;
    }

    if (Input.GetAxis("Mouse Y") != 0 && mouseDown)
    {
        yForce -= Input.GetAxis("Mouse Y") * 50;
    }      

    if (Input.GetMouseButtonUp(0))
    {
        GetComponent<Rigidbody2D>().gravityScale = 1;
        GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce));
        yForce = 0;
        xForce = 0;
    }

But while working perfectly while testing it on unity using the mouse, it behave strangely on my 2 android phones. Sometimes the ball seems to get the force only for the X axis, and even if dragging only vertically the ball has an X force applied… What could be the problem?

EDIT: I added Debug.Log(xForce) and Debug.Log(yForce) and looking at the log with adb logcat I noticed that, also when simply tapping on the screen, without dragging on neither axis, the value of those variables increase, I have really no idea on how to solve this…

private float yForce;
private float xForce;
private float maxForceY = 900;
private float maxForceXp = 800;
private float maxForceXm = -800;
private float startPosX;
private float startPosY;
private float endPosX;
private float endPosY;
private float originX;
private float originY;

    void Update () {          

         originX = GetComponent<Rigidbody2D>().position.x;
         originY = GetComponent<Rigidbody2D>().position.y;
        
        if (Input.GetMouseButtonDown(0)) 
        {            
            startPosX = Input.mousePosition.x;
            startPosY = Input.mousePosition.y;                
        }
                  
        if (Input.GetMouseButtonUp(0))
        {               
            endPosX = Input.mousePosition.x; 
            endPosY = Input.mousePosition.y;

            xForce = (startPosX - endPosX) * 3;
            yForce = (startPosY - endPosY) * 2;

            if (yForce < 0) yForce = 0;
            if (yForce > maxForceY) yForce = maxForceY;
            if (xForce > 0 && xForce > maxForceXp) xForce = maxForceXp;
            if (xForce < 0 && xForce < maxForceXm) xForce = maxForceXm;

            GetComponent<Rigidbody2D>().gravityScale = 1;
            GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce)); 
                       
            yForce = 0;
            xForce = 0;
            startPosX = 0;
            startPosY = 0;   
            endPosX = 0;
            endPosY = 0;             
        }           
    }   

This is what I ended up with, hope it helps. Surely, it can be improved :wink:

Those values for Input.GetAxis are delta values, so it is the difference in movement of the mouse since the last update frame. It may not always be accurate. What I would recommend you doing, however, is get the start position of the mouse when they first click, and the end position when they release. I would get the difference between that and simply use that as the x and y force values. So something like this:

Vector2 start, end, force;

void Update() {
    if(Input.GetMouseButtonDown(0)) start = Input.mousePosition;
    else if(Input.GetMouseButtonUp(0)) {
        end = Input.mousePosition;
        force = end - start;
    }
}

If you want to try that out, let me know how that works. It is just a start, so I do not know how it will work in your project.

Hey @RingK ,

Can you post the full script so that those of who are learning can get the full reference? Would be super super helpfu!