1
\$\begingroup\$

My Player moves on Y AXIS , but sometimes it doesnt go up but stucks in one place, Script is like in swing copters , when you touch it rotates to another side but its on X Axis , i took and made it on Y axis , but it doesnt work properly , player sometimes doesnt turn around and go up or back Please help out , sorry for bad english

void MovePlayerOnYAxis()
    {

        foreach (Touch touch in Input.touches)
        {


            moveL = !moveL;

            if (moveL)
            {

                transform.localScale = new Vector3(1, -1, 1);
            }
            else
            {
                transform.localScale = new Vector3(1, 1, 1);
            }

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

Pre-Fix From @PompeyPaul But Player still doesnt go Down , it now goes only up.

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);
    }
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Is it because you flip direction on each touch? So if you get multiple touches you flip up and down.

I think you need to debounce the touches, something like

    bool WasTouched=false; // Should go into the class

    // Debounce touches
    if(wasTouched)
    {
        if(Input.touches.Count==0)
        {
            WasTouched=false;

            // Do processing here
        }
    }
    else
    {
        if(Input.touches.Count>0)
        {
            WasTouched=true;
        }
    }
\$\endgroup\$
1
  • \$\begingroup\$ i have everting setup input.touches e.t.c but players sometimes glitches it doesnt turn around \$\endgroup\$ Commented Nov 1, 2016 at 21:57
0
\$\begingroup\$

The Translate function has a second default parameter, which is the relative space it moves to. If not set, this will be Space.Self.

Since you set the y-component of localScale to -1, this will automatically account for moving different directions in world space, thus, making huongbay inside of Translate redundant.

You can either remove it, or set the relative space to Space.World:

transform.root.Translate(Vector3.up * speed * Time.deltaTime);
// or
transform.root.Translate(Vector3.up * speed * huongbay * Time.deltaTime, Space.World);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .