Move player same distance every touch

Hi
I want my player to move A to B, B to C by touching right arrow and C to B, B to A by touching left arrow as shown in attached image. Because my player wants to catch balls come from “*” position marked as attachment. I used two difference method for implement player movement. But It doesn’t stop at A,B or C position all time. Some time it goes far or less. Event Trigger is used to arrow touch detection.

My target is for android device and I use unity 5.4 and c# to develop my game. It’s 2D game.

86051-screenshot.png

The Code I used is put below. I have commented the second method I used.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float leftBoundry, rightBoundry;
}
public class MovePlayer : MonoBehaviour
{

    public Boundary boundary;

    public float speed;
    Rigidbody2D playerRigBody;
    Vector2 movement;
    float input = 0;

    void Start()
    {        
        playerRigBody = this.GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        PlayerMovement(input);
    }

    void PlayerMovement(float horisontalInput)
    {
        //movement.x = playerRigBody.velocity.x;
        //playerRigBody.velocity = movement;
        //movement = playerRigBody.velocity;
        //movement.x = horisontalInput * speed * Time.fixedDeltaTime;
        //playerRigBody.velocity = movement;


        movement = playerRigBody.position;
        movement.x = movement.x + (0.4f * horisontalInput);
        playerRigBody.position = movement;

        
        playerRigBody.position = new Vector2
       (
           Mathf.Clamp(playerRigBody.position.x, boundary.leftBoundry, boundary.rightBoundry),
           -3.77f
       );
    }

    public void UserInput(float horisontalInput)
    {
        //prevent player move beyond the boundy after met boundry
        //if ((horisontalInput == 1 && playerRigBody.position.x >= boundary.rightBoundry) || (horisontalInput == -1 && playerRigBody.position.x <= boundary.leftBoundry))
        //{
        //    input = 0;
        //}
        //else {
        //    input = horisontalInput;
        //}

        input = horisontalInput;
    }
}

Hey, If I were you, probably I would make a public float array that would contain all the X-Positions (For Example : -5f,0f and 5f), and I would have a variable called index which will increment till the length of the array while you’re pressing the right button and vice versa.

And in the end, i would use a MoveTowards function to move at particular position in x axis, keeping the relevant positions at the same point.

I am writing a pseudocode here :-

float []  xPoints = new float[3]{-5f,0,5f};
int index = 0;

void RightClick(){
if(index is less than the length of the array minus 1)
  index ++;
}

void LeftClick(){
//Vice versa of RightClick function.
}

and in the update function…

transform.position = Vector3.MoveTowards(transform.position, new Vector3(xPoints[index], transform.position.y, transform.position.z), Time.deltatime * 10f);

Hope this helps in your case. Cheers !!!

My solution using @iamsidv’s pseudocode

using UnityEngine;
using System.Collections;

public class MovePlayer : MonoBehaviour
{
    float[] xPointsArray;

    int pointIndex = 0;

    public float speed;
    Rigidbody2D playerRigBody;

    void Start()
    {
        xPointsArray = new float[3] { -4f, 0, 4f };    
        playerRigBody = this.GetComponent<Rigidbody2D>();
    }
   
    void FixedUpdate()
    {
        PlayerMovement();
    }

    void PlayerMovement()
    {
        playerRigBody.transform.position = Vector3.MoveTowards(transform.position, new Vector3(xPointsArray[pointIndex], transform.position.y, transform.position.z), Time.deltaTime * speed);

    }

    public void RightClick()
    {
        if (pointIndex < (xPointsArray.Length - 1) && pointIndex != 2)
        {
            pointIndex++;
        }
    }

    public void LeftClick()
    {
        if (pointIndex <= (xPointsArray.Length - 1) && pointIndex != 0)
        {
            pointIndex--;
        }
    }

}