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:
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…
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.