touchPhase.Ended not working

So I tried using TouchPhase.Ended inside the if statement (input.touchcount > 0), which didn’t work so after looking through the forums the solution seemed to be to create a boolean instead, so I did that and here is the result. However, the seperate if statement that I used to replace the TouchPhase.Ended is also not working. Any suggestions on how to fix this (note I also tried using a switch case for phase ended and moving etc which also did not work) any suggestions help im very stuck :frowning: (P.S. every part of the code is working perfectly fine up until the point where it should be determining if the finger is lifted)

void Update()

    if (Input.touchCount > 0)
        touchended = false;
        Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero
        if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Began)
            currentDistance = Vector3.Distance(touchPointA.transform.position, transform.position);
            if(currentDistance <= maxDistance)
                safeSpace = currentDistance;
                safeSpace = maxDistance;

            //calc power and direction
            shootPower = Mathf.Abs(safeSpace) * 10;

            Vector3 dimxy = touchPointA.transform.position - transform.position;
            float difference = dimxy.magnitude;

            touchPointB.transform.position = transform.position + (dimxy / difference * currentDistance * -1);
            touchPointB.transform.position = new Vector3(touchPointB.transform.position.x, touchPointB.transform.position.y);

            shootDirection = Vector3.Normalize(touchPointA.transform.position - transform.position);



    if (touchended == false)
        Vector3 push = shootDirection * shootPower;
        GetComponent<Rigidbody>().AddForce(push, ForceMode.Impulse);
        touchended = true;

Well, it’s hard to help you with this. First of all I used Ended / Canceled successfully in the past, so I have no idea what issues you actually have with it. You didn’t show your code with TouchPhase.Ended so we can’t tell what might be wrong. Note that a touch does not necessarily end with TouchPhase.Ended. TouchPhase.Canceled is also a way how a touch can end.

Next thing is your code will only work with single touch events. If you want to allow the user to have more than one touch active at the same time this doesn’t work. Every touch has a “fingerID” which actually identifies a certain tracked touch.

You haven’t linked the forum post that suggested the workaround you used. However you clearly missed an important part. Currently your second if statement will just run in parallel to your first one. Because if you enter the first one you set your bool to false. This will execute the second if statement after the first one. Here you set the bool to true. However as long as there is a touch you set your bool back to false every frame and therefore you will execute your second if body every frame. Once there is no touch anymore your bool will stay true. So your second if statement is pretty pointless.

Your question title is about TouchPhase.Ended doesn’t work but your question doesn’t use it at all. It’s also generally helpful to know what your target platform is (iOS, Android or WindowsPhone?)

Finally I guess what your workaround is missing and it’s the “else” after the first if statement. The point is that as long as there is a touch we reset the “touchended” variable. However since the second if statement is in the “else” of the first one it will not be evaulated. Once there is no touch anymore the first if will be skipped and we actually check the second one. There “touchended” is false and we enter the second if body and set touchended to true to ensure it only runs once.