0
\$\begingroup\$

i am making script something like in swing copters when player moves to Left to right when you touch the screen , but its on X asis , i made my script on Y Axis , But it only goes down when i touch screen , it just doesnt go up at all , Please help

void MovePlayerOnYAxis()
    {

        foreach (Touch touch in Input.touches)
        {

            WasTouched = !WasTouched;


            if (WasTouched)
            {
                if (Input.touchCount == 0)
                {
                    WasTouched = false;

                    transform.localScale = new Vector3(1, 1, 1);
                }
            }
            else
            {
                if (Input.touchCount > 0)
                {
                    WasTouched = true;
                    transform.localScale = new Vector3(1,-1, 1);
                }

            }
            huongbay = transform.localScale.y;

        }
        transform.root.Translate(Vector3.up * speed * huongbay * Time.deltaTime);
    }

Input script

bool WasTouchedOrClicked()
    {
        if(Input.GetButtonUp("Jump") || Input.GetMouseButtonDown(0) ||
         (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
            return true;
        else
            return false;
        }
\$\endgroup\$
  • \$\begingroup\$ @DMGregory i AM newbie to programming , i can send FULL CODE of my script \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:33
  • \$\begingroup\$ No, don't do that, just get rid of this foreach loop. Use a foreach only if you want to repeat some code for every item in a list. Here, you don't want to repeat your scale code for every simultaneous touch (eg. if I touch the screen with 5 fingers, you don't really want to change the player's scale 5 times) \$\endgroup\$ – DMGregory Nov 2 '16 at 16:35
  • \$\begingroup\$ @DMGregory Its means that i need to Delete Foreach? \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:36
  • \$\begingroup\$ @DMGregory I just deleted Foreach , But it still doesnt go up , its just going down \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:38
1
\$\begingroup\$

I would say it's because you always toggle the WasTouched flag. Try removing the line:

WasTouched = !WasTouched;
\$\endgroup\$
  • \$\begingroup\$ its still doesnt help everything the same \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:01
  • \$\begingroup\$ Did you try putting a breakpoint into the line: WasTouched = false;? My guess is that Input.touches is empty when there are no touches and thus the code is never reached \$\endgroup\$ – wakka Nov 2 '16 at 16:12
0
\$\begingroup\$

This looks like a copy-paste error from an earlier answer. What the other user intended you to write was something more like this:

bool wasTouched = false;
float direction = 1f;

void MovePlayerOnYAxis()
{


    if (wasTouched)
    {
        if (Input.touchCount == 0)
        {
            WasTouched = false;
            direction = 1f; // up
        }
    }
    else
    {
         if (Input.touchCount > 0)
         {
             WasTouched = true;
             direction = -1f; // down
         }
     }

    transform.root.Translate(
           Vector3.up * speed * direction * Time.deltaTime, 
           Space.World);
}

As discussed in the comments above, the behaviour you've described should only depend on whether the screen was touched - not how many touches there are. So, you don't want to loop over every touch with a foreach - running this logic just once is enough.

In fact, if all you want to do is go down when there's at least one touch, you can simplify this massively to:

void MovePlayerOnYAxis()
{
    float direction = Input.touchCount > 0 ? -1f : 1f;

    transform.root.Translate(
       Vector3.up * speed * direction * Time.deltaTime, 
       Space.World);
 }
\$\endgroup\$
  • \$\begingroup\$ Now it goes up but doesnt go down only player sprite turn down , but not player moving down \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:43
  • \$\begingroup\$ if you want i can send you in private my script for player \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:46
  • \$\begingroup\$ This can occur if the transform is its own root. Because you've flipped the sprite, telling it to go "down" in its own coordinate space means actually going up. Changing to Space.World as I've done above would remove this ambiguity. \$\endgroup\$ – DMGregory Nov 2 '16 at 16:47
  • \$\begingroup\$ i just need something like this youtube.com/watch?v=NdYsVifYdvc but it should go on Y AXIS, and without speedup \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:48
  • \$\begingroup\$ i am just a newbie to programming so dont get something what are you saying :( \$\endgroup\$ – Peteris Ulmanis Nov 2 '16 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.