- Home /
Question by
JPsLucky13 · May 19, 2015 at 08:04 PM ·
movementmobiletouchcoroutinewhile loop
IOS Touch movement not exiting coroutine
Hey guys I' running a player movement touch coroutine to show the player's movement. I'm changing up a Rigidbody2D.MovePosition in a while loop within a coroutine. The player's movement is based on the detection of a touch on the screen.
The main issue is the instructions inside the while loop keep executing after the condition is not met.
Would appreciate you help!! Movement is working fine on Android build.
private int timePassed; public int moveFrames;
void Update () { timePassed = Time.frameCount;
Vector3 referenceForward = Vector3.right;
Vector3 referenceRight = Vector3.Cross(Vector3.up, referenceForward);
Vector3 newDirection;
if (Input.touchCount > 0)
{
userTouch = true;
}
else
{
userTouch = false;
}
//Reads Touch and Player Position
if (userTouch) {
//Start COunting the frames
//unitMovement = 1f;
touchPosition = Input.GetTouch(0).position;
playerPosition = Camera.main.WorldToScreenPoint(GetComponent<Rigidbody2D>().position);
newDirection = touchPosition - playerPosition;
angle = Vector3.Angle(newDirection, referenceForward);
sign = Mathf.Sign(Vector3.Dot(newDirection,referenceRight));
finalAngle = sign * angle;
//Debug.Log ("Angle:" + finalAngle);
StartCoroutine("Movement");
/*
Debug.Log ("Marcelino Positions:");
Debug.Log(playerPosition.x);
Debug.Log(playerPosition.y);
Debug.Log ("Touch Positions");
Debug.Log (touchPosition.x);
Debug.Log (touchPosition.y);
*/
}
//Check if player exited the app
if (Input.GetKeyDown (KeyCode.Escape)) {
Application.Quit ();
}
}
IEnumerator Movement(){
moveFrames = timePassed + 6;
//Move Forward
if (touchPosition.y > playerPosition.y)
{
if(finalAngle > 45.0f && finalAngle < 135.0f)
{
while(timePassed < moveFrames)
{
GetComponent<Rigidbody2D>().MovePosition(GetComponent<Rigidbody2D>().position + (Vector2.up*unitMovement*Time.deltaTime*0.5f));
yield return null;
}
}
}
}
Comment