- Home /
Issue with Input.GetMouseButton (0) on mobile (iPhone)
In my game, if a user holds down the left mouse button, a jetpack will rise in the air with flames ignited. This thrust actively works against gravity. When the thrust turns off, the flames will go out and gravity will cause the jetpack to fall.
This works perfectly in Unity. On my iPhone, however, the jetpack will not lift off the ground. Instead is stutters on the ground when the screen is being touched. The flames, however, do show and hide correctly when the screen is being touched / not touched, therefore I know it is reading the input correctly. Perhaps there seems to be an issue with the physics that only happens on the phone?
Does anyone have any ideas? Thanks!
void Update () {
if (Input.GetMouseButton (0)) {
// Show flames
thrustObject.SetActive(true);
// Force that works when using the mouse, but not when on my iPhone screen
rb.AddForce (new Vector2 (0, thrust), ForceMode2D.Impulse);
} else if (Input.GetMouseButtonUp(0)) {
// Hide flames
thrustObject.SetActive(false);
}
}
Answer by kamiccolo · Mar 31, 2016 at 10:01 AM
FixedUpdate seems to be necessary on mobile. My guess is that it has to do with varying frame rates on different devices that perform differently. The fact that there is no Time tracking on ForceMode2D.Impulse makes me lean more towards this assumption.
void FixedUpdate () {
if (Input.GetMouseButton (0)) {
// Show flames
thrustObject.SetActive(true);
// Force that works when using the mouse, but not when on my iPhone screen
rb.AddForce (new Vector2 (0, thrust), ForceMode2D.Impulse);
} else if (Input.GetMouseButtonUp(0)) {
// Hide flames
thrustObject.SetActive(false);
}
}
The reason why this happens is that Update is called before every frame update, whereas FixedUpdate is called before every physics update. The frame rate may change throughout the game which would cause Update to be called less frequently, but FixedUpdate is called in the same interval regardless of the frame rate. So the visual part works correctly because it is called before the screen renders, but the stuttering happens because the force is added only on Update, which is not necessarily called on every physics update.
Answer by ShadoX · Mar 31, 2016 at 07:41 AM
Try having a look at http://docs.unity3d.com/Manual/MobileInput.html
Input.Get$$anonymous$$ouseButtonUp(0) does work as a valid default input on mobile devices. Also the phone is reading the input just fine, as seen with the jetpack's flames. Thanks anyway though.
Your answer
Follow this Question
Related Questions
Tie touch logic into InputManager 0 Answers
How to differentiate touches on mobile devices 1 Answer
Mobile - button press while moving (First Person Controls) 0 Answers
Does Unity Remote 4 act the same as it would when it is on the app store? 1 Answer
Input.mousePosition equivalent to first finger touch? 3 Answers