Using transform.position and Mathf.PingPong on an object how have a random x position

Hi everybody,

So i’m triying to move an object from a point(A) to another point(B) and make him go back again to the first point (A) using Mathf.PingPong but the problem is that the point(A) have a Random value.

Here’s my script :

using UnityEngine;
using System.Collections;

public class MouvementObstacleCarre : MonoBehaviour {
public Vector2 vitesse = new Vector2(0,-2);
Vector2 vec = new Vector2(0,-1f);
public float range =1.7f;
Vector3 drag = new Vector3 ();

void Start ()
{
if (PlayerBox.isFristJump) // true when the player click on the screen for the first time
{

GetComponent<Rigidbody2D>().velocity = vitesse;
transform.position = new Vector3 (transform.position.x - range * Random.value, transform.position.y, transform.position.z);

}

}

void Update()
{

if (Score.CompteurScore >= 5 ) // when the player score 5 points then the movement begin
{ drag = transform.position;

Debug.Log(drag.x);
transform.position = new Vector3 (PingPong (Time.time, drag.x, -9.42f), transform.position.y, 0);

}
}


float PingPong(float aValue, float aMin, float aMax)
{
return Mathf.PingPong(aValue, aMax-aMin) + aMin;
}
}

the problem is with the drag.x : he doesn’t take the last position of the transform and start the movement with it.
Is it possible to start Mathf.PingPong with the last position of the point(A) just before the player score 5 points and make him go slowly ( without teleport) to the position of the point(B) wich is -9.42. I tried many solutions like Vector.lerp but nothing worked.

I’m stuck on this for like 3 days and i really need some help.

Thank you.

As mentioned in my comment, moving the object “manually” should be a more flexible solution. Something like this (attach this script to camera in an empty scene)

using UnityEngine;
using System.Collections;

public class PingPongMover : MonoBehaviour {

	Transform _Mover;

	// limits
	public float _MinX = -0.5f; 
	public float _MaxX = 1.1f; 

	// a varible to adjust movement speed
	public float _Speed = 2f;

	// a varible to adjust movement direction (1 or -1)
	int _Direction = 1;

	void Start ()
	{
		_Mover = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
	}
	
	void Update ()
	{
		// how much to move in this update
		Vector3 moveSpeed = _Speed * Vector3.right * Time.deltaTime * _Direction;

		_Mover.position += moveSpeed;

		// check if inside limits
		if (_Mover.position.x < _MinX)
		{
			// how much are we over the limit (well actually the negative of it)?
			float over = _MinX - _Mover.position.x;

			// move the object back the amount it went over the limit
			// otherwise it will move less this turn and it looks like 
			// the movement is jerky
			_Mover.position += Vector3.right * over;

			// flip movement direction if it's moving further out of bounds
			if (_Direction < 0) 
				_Direction = 1;
		}
		else if (_Mover.position.x > _MaxX)
		{
			float over = _MaxX - _Mover.position.x;

			_Mover.position += Vector3.right * over;

			if (_Direction > 0) 
				_Direction = -1;
		}
	}
}