# Unity Movement Help

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;
}

• @DMGregory i AM newbie to programming , i can send FULL CODE of my script – Peteris Ulmanis Nov 2 '16 at 16:33
• 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) – DMGregory Nov 2 '16 at 16:35
• @DMGregory Its means that i need to Delete Foreach? – Peteris Ulmanis Nov 2 '16 at 16:36
• @DMGregory I just deleted Foreach , But it still doesnt go up , its just going down – Peteris Ulmanis Nov 2 '16 at 16:38

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

WasTouched = !WasTouched;

• its still doesnt help everything the same – Peteris Ulmanis Nov 2 '16 at 16:01
• 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 – wakka Nov 2 '16 at 16:12

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);
}

• Now it goes up but doesnt go down only player sprite turn down , but not player moving down – Peteris Ulmanis Nov 2 '16 at 16:43
• if you want i can send you in private my script for player – Peteris Ulmanis Nov 2 '16 at 16:46
• 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. – DMGregory Nov 2 '16 at 16:47
• i just need something like this youtube.com/watch?v=NdYsVifYdvc but it should go on Y AXIS, and without speedup – Peteris Ulmanis Nov 2 '16 at 16:48
• i am just a newbie to programming so dont get something what are you saying :( – Peteris Ulmanis Nov 2 '16 at 16:49