1
\$\begingroup\$

I have a script that randomly chooses a position to move a game object every 5 seconds, however instead of moving my object smoothly, my script make my object jumps, which is not desirable! How would I make my object move to position smoothly! Thank you!

public float timer;
public float speed;


public int newtarget; 


public float xPos;
public Vector3 desiredPos;


void Start() {
}

void Update () {
    timer += Time.deltaTime;
    if (timer >= newtarget) {
        newTarget ();
        timer = 0;
    }
}

void newTarget() {
    xPos = Random.Range(-4.5f, 4.5f);
    desiredPos = new Vector3(xPos, transform.position.y, transform.position.z);

    transform.position = Vector3.Lerp (transform.position, desiredPos, Time.deltaTime * speed);
        if (Vector3.Distance(transform.position, desiredPos) <= 0.01f)
        {
            xPos = Random.Range(-4.5f, 4.5f);
            desiredPos = new Vector3(xPos, transform.position.y, transform.position.z);
        }
}
\$\endgroup\$
3
\$\begingroup\$

Before I go into the code, I want to go over how Lerp works.

Vector3.Lerp(Vector3 start,Vector3 end, float fraction);

The above code will return a Vector3 that is between the start and end positions at some fraction of the way along a line between two points. So:

transform.position=Vector3.Lerp(Vector3 start,Vector3 end, float t);

If t is 0.5f, this will teleport the object right between start and end, if t is 0.25f, the object will be teleported to a quarter of the way along the line from start to end. So you want to "move" your object by changing it's position little by little using Update(), and keep track of your start position and distance to your desired position.

You can find more information about Lerp at the Unity Docs reference page for Lerp

private Vector3 startPosition; //You will need to keep track of your initial position as well as your desired position
private float startTime;//You will need to get the time when the movement has started
private bool moving;//You will need to keep track of when the object is moving or not

public float timer;
public float speed;
public int newtarget;
public float xPos;
public Vector3 desiredPos;

void Update()
{
    timer += Time.deltaTime;
    if (timer >= newtarget)
    {
        NewTarget();
        timer = 0;
    }
    if (moving) //This part of the code will execute when moving is set to true in NewTarget()
    {
        float timePast = Time.time - startTime; //How many seconds has it been since moving was set to true
        float distanceCovered = timePast * speed; //Make sure to keep speed small, when distanceCovered reaches 1, the movement will be over
        //The object will arrive at its desired position in 1/speed seconds
        transform.position = Vector3.Lerp(startPosition, desiredPos, distanceCovered); //when distanceCovered is 0, the object will be on startPosition, when it is at 1, the object will be on desiredPos
        if (distanceCovered >= 1)//If the object reached the end, it is not moving anymore, if you want the wait to always last newtarget amount of seconds, you might want to reset the timer here instead and change if(timer >= newtarget) to if(timer >= newtarget && !moving)
        {
            moving = false;
        }
    }
}

void NewTarget()
{
    //I tried to keep as much of your code as possible and I am assuming you only want to move the object in the X axis in this case
    xPos = Random.Range(-4.5f, 4.5f);
    desiredPos = new Vector3(xPos, transform.position.y, transform.position.z);


    moving = true;//The new target is acquired so the movement begins
    startPosition = transform.position; //Keep track of the object's original position
    startTime = Time.time; //Remember the moment when the movement started so you can calculate the distance
    distance = Vector3.Distance(startPosition, desiredPos);//Get the distance to your desired position in float

    //You need to do your Lerping in Update() instead
}

I hope this is helpful.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Woow thank you for the help, I'm going to learn a bit more on this subject! I really appreciate the help! \$\endgroup\$ – Bakausagi-chan Aug 25 '18 at 18:11

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.