Hi, I’m new to the C++ language and Unity. This is what I’m trying to accomplish, move an Object through 2 points starting from x0 and y0 moving to x= -11 y= 4 then from this position to x=0 y=2, but when I try my Object stop moving at x=-5.444 an y=2.
This is my code.
public Vector2 target1 = new Vector2 (-11,4);
public Vector2 target2 = new Vector2 (0,2);
public Vector2 target2 = new Vector2 (0,2);
public float speed=1;
public float i=0;
private static Vector3 puntoA ;
private static Vector3 puntoB ;
// Use this for initialization
void Start () {
puntoA = new Vector3(target1.x, target1.y,0) - transform.position ;
puntoB = new Vector3(target2.x, target2.y,0) - transform.position ;
}
// Update is called once per frame
void Update () {
Debug.Log("i: " +i);
if (i==0)
{
if ( (transform.position.x >= puntoA.x) & (transform.position.y <= puntoA.y) ) //x della Meella maggiore a -11 e y inferiore a 4
{
transform.Translate (puntoA.normalized* speed* Time.deltaTime) ;
}
else
{
++i;
}
}
if (i==1)
{
if ( (transform.position.x <= puntoB.x) & (transform.position.y >= puntoB.y) ) //x della Mella inferiore a 0 e y maggiore a 2
{
transform.Translate (puntoB.normalized* speed* Time.deltaTime) ;
}
else
{
++i;
}
}
}
}