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’m stuck on this for like 2 days and i really need some help.
Thank you.