UnityLover thanks for providing me with a head start with a mobile control. I’ve included it in my game code and modify it to support multiple touches but I’m having a strange issue. When I press left and right fast and then jump then the character keeps on walking to either right or left direction making me think that moveLeft or moveRight never get sets to false in some instances.
I read a lot of posts talking about TouchPhase.Ended not firing, have you guys experience this? is there a solution?
Here’s my code:
if (Input.touchCount > 0) {
var touchCount = Input.touchCount;
for (var i = 0; i < touchCount; i++) {
var t = Input.GetTouch (i);
if (t.phase == TouchPhase.Began) {
if (guiLeft.HitTest (t.position, Camera.main))
moveLeft = true;
if (guiRight.HitTest (t.position, Camera.main))
moveRight = true;
if (guiJump.HitTest (t.position, Camera.main))
{
Debug.Log("Jump pressed");
if (groundHit !stickmanIsDead) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
//Record start time only once
holdJumpDownStartedAt = Time.time;
}
}
//Fall from Platforms
if(t.phase == TouchPhase.Stationary){
if (guiJump.HitTest (t.position, Camera.main)){
if(Time.time - holdJumpDownStartedAt > dropPlatformAt){
Debug.Log(string.Format("Holded for {0} second :) ",dropPlatformAt.ToString()));
holdJumpDownStartedAt = 0.0f;
#region Falling Keystroke
var playerFalling = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Floor-Air"));
//Fall Key
if (playerFalling)
StartCoroutine (Fall ());
#endregion
}
}
}
if (t.phase == TouchPhase.Ended) {
if (guiLeft.HitTest (t.position, Camera.main))
moveLeft = false;
if (guiRight.HitTest (t.position, Camera.main))
moveRight = false;
}
}
}
It’s just a guess, but since you said you were doing it fast, the touch.phase may be getting skipped if you get your finger down on the jump before it checks. Since the only place you appear to setting the move bools is on that condition, they will stay active. You might try this instead:
if (guiLeft.HitTest (t.position, Camera.main))
moveLeft = true;
if (guiRight.HitTest (t.position, Camera.main))
moveRight = true;
Also, in your touch.phase end loop, you are only setting them to false if the hit test is true, which it shouldn’t works as touch.ended and hittest==true should never be valid.
Try changing:
if (t.phase == TouchPhase.Ended) {
if (guiLeft.HitTest (t.position, Camera.main))
moveLeft = false;
if (guiRight.HitTest (t.position, Camera.main))
moveRight = false;
}
Oops, ignore the first suggestion, that will most likely fail, or at least not work as expected. As you are not doing it by id, it will flip the true/false on every test.
Here, this would be more reliable, as it will ensure that if nothing is being touched, nothing will be true.
if (Input.touchCount > 0) {
<... the code ...>
}
else
{
moveRight = moveLeft = false;
}
Ah, I see, you want to have both down. To clarify:
Player presses right button : character runs right until released.
Player presses and holds right button, and taps jump, still holding right button, character runs right, jumps moving right and continue moving right.