1
\$\begingroup\$

Pretty new to Unity and i'm trying to make a very simple iphone game. , I'd like to know how to move a sprite (2D) move from random point A to random Point B not just that, but i want it to change direction halfway through.

here is my attempt:

public float speed;
public float minForce;
public float maxForce;
public float counter;
private float counter2;

void Start () 
{
    Push ();

}

void Update ()
{
    counter2 -= Time.deltaTime;
    if ( counter2 < 0)
    {
        Push ();
        counter2 = counter;

    }

}


void Push ()
{
    float force = Random.Range(minForce, maxForce);
    float x = Random.Range(-1f, 1f);
    float y = Random.Range(-1f, 1f);

    rigidbody2D.AddForce( force * new Vector2(x, y) * speed);
}

}

I'm not quit satisfied with using force, also tried Vector2.lerp but for some reason it just teleported instead of smoothing. Also i want to make it randomly change position only once.

here is my perp attempt:

 private Vector2 startPoint ;


public float speed ;
public float counter;
private float counter2;

void Start () 
{
    startPoint = transform.position;
}

// Update is called once per frame
void Update () 
{
    counter2 -= Time.deltaTime;
    if ( counter2 < 0)
    {
        RandomPosition ();
        counter2 = counter;

    }

}

void RandomPosition ()
{
    float counter = speed * Time.deltaTime;
    float x = Random.Range(-50f, 50f);
    float y = Random.Range(-100f, 100f);
    Vector2 endPoint = new Vector2(x, y);
    transform.position = Vector2.Lerp(transform.position, endPoint,       Time.deltaTime * speed);  
}
}
\$\endgroup\$
1
\$\begingroup\$

So, first: If you want to use Vector2.Lerp, you have to call it every frame(Update), with a parameter between 0 and 1 - what you did, is create a new line, Lerp to some random point on it, and discard everything on the next frame.
You also used a lot of local variables for stuff that needs to be class members, because other functions need access to them.

A simple way to do what you want would be

float timer;
Vector3 startPos;
Vector3 endPos;

void Start() 
{
   RandomPosition();
}

void RandomPosition()
{
  timer = Time.time;
  startPos = transform.position;
  endPos = new Vector3(Random.Range(-50f, 50f), Random.Range(-100f, 100f), 0);
}

void Update()
{
  if (Time.time - timer > 1)
  {
    RandomPosition();
  }
  transform.position = Vector3.Lerp(startPos, endPos, Time.time - timer);
}

This is if course quite trivial, p.e. it moves the transform from a to b in one second, without caring about speed, but should get you on the right direction.

Also, if you want to just change direction half way to a new random direction - is that not the same as simply moving only half the way, and then starting again? No need to make it any more complicated.

\$\endgroup\$
  • \$\begingroup\$ This code don't work for me, it gives me an error for the { in IF statement. Also i don't understand the Time.time - timer > 1 how is it different from the counters i used ? Also if you know a simpler code t achieve my goal please, feel free to provide it. \$\endgroup\$ – Abdou23 May 13 '14 at 0:59
  • \$\begingroup\$ That error is probably because I accidentally used uppercase for the if, which the compiler will tell you. You need to be able to fix errors like that on your own. If you cannot, you should get some programming books and start with coding, not programming games. Remember that answers should answer your question, not provide you will a full, copy-paste-able solution. Btw, there is a difference between timer += Time.deltaTime and Timer.time - startTime > x - as your way adds a (very small) rounding error every frame. \$\endgroup\$ – kat0r May 13 '14 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.