Why the gameobject is moving so fast between the random waypoints ?

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class SpawnObjects : MonoBehaviour
{
    public int numberOfObjects;
    public GameObject objectToPlace;
    public Vector3 newObjectsSize = new Vector3(5, 5, 5);
    public float speed;

    private int wallsLengthX;
    private int wallsLengthZ;
    private int wallsPosX;
    private int wallsPosZ;
    private int currentObjects;
    private List<GameObject> objects = new List<GameObject>();

    void Start()
    {
        var wi = GetComponent<WallsTest>();
        wallsLengthX = (int)wi.lengthX;
        wallsLengthZ = (int)wi.lengthZ;
        wallsPosX = (int)wi.wallsStartPosition.x;
        wallsPosZ = (int)wi.wallsStartPosition.z;
    }
    // Update is called once per frame
    void Update()
    {
        Spawn();
        MoveObjects();
    }

    private void Spawn()
    {
        if (currentObjects != numberOfObjects)
        {
            GameObject newObject = (GameObject)Instantiate(objectToPlace);
            newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
            newObject.transform.localPosition = GenerateRandomPositions(newObject);
            objects.Add(newObject);

            currentObjects += 1;
        }
    }

    private Vector3 GenerateRandomPositions(GameObject newObject)
    {
        float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
        float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ) / 2f;
        float originX = wallsPosX + paddingX - wallsLengthX / 2f;
        float originZ = wallsPosZ + paddingZ - wallsLengthZ / 2f;
        float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX);
        float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ);
        float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));

        return new Vector3(posx, posy, posz);
    }

    private void MoveObjects()
    {
        //objects[0].transform.position = Vector3.MoveTowards(objects[0].transform.position, positions[+1], speed * Time.time);
        objects[0].transform.position = Vector3.Lerp(objects[0].transform.position, GenerateRandomPositions(objects[0]), speed * Time.time);
    }
}

First time i call Spawn() and spawning the objects in random positions.
Then i’m calling MoveObjects and for the testing i’m moving for now only the first newObject in the List. objects. objects[0]

The problem is even if i set the speed to 0.1 or to 20 the speed is too much fast. If i will do 0.0001 the speed will be slowly but then it will move faster again.

I checked and the speed is not changing keep to be 0.1 or 20.
What i’m doing is moving the object inside the walls area only.
I also checked the new random position it’s generating each time and it’s randomly.
But still the object is moving on some specific small area then the walls area for example if the walls area is 500x500 in this case also the terrain area the object is moving on small area and never get to far places around. And it’s keep moving very fast.

I tried to use MoveTowards and then Lerp but it’s the same problem.

I think you want to use Vector3.MoveTowards in this case. Using Vector3.Lerp like this the object will start moving fast and then slow down as it gets closer to the destination. Also you need to change Time.time to Time.deltaTime.

1 Like

When changing to Time.deltaTime the speed is working now. But for some reason if i set the speed to 0.1 to move very slow it’s just making the object to move on very very small area it’s like the positions are narrow for very small area and in this small area it looks like the cube is moving fast just in very small area. But if the change the speed to 100 i see the object moving very very fast but all around the walls area like i wanted.

Strange.

This is a very short video clip i recorded now show the cube movement behaviour when using both Vector3.MoveTowards or Vector3.Lerp.

In both cases when i change the speed in Lerp to fast very fast it will move fast around the walls area when i change the speed to very slow the cube will move to the center and move fast on very limit area. But the cube should move slow all around the area between the walls. Instead it’s moving fast but on small area. Not sure why.

Same when using MoveTowards.

The behaviour that i want the cube to move is, is when i change the speed to very very slow or fast or ver very fast move to random positions all around the walls area. Inside the walls area.
In the video the walls are 500x500 but the cube is moving on very very small area.

You did not describe precisely enough what kind of movement you’re aiming for.

The reason for this strange behaviour is that you use Lerp incorrectly, and that you generate a new target position every Update. That is, it’ll generate a new position and move one step towards it, next frame, it’ll generate yet another new position, steps towards it etc…

I could imagine you want to have it moving to a target position completely before it generates a new one, but that’s just a guess.

1 Like

I think you should double check the Lerp docs. Unity - Scripting API: Vector3.Lerp
The third value is a value between 0 and 1, though it does clamp it, if you are above 1, it will always be 1. The third value basically states how far you want the object to move towards the next position. If the third value is .5, it will move 50% of the way. Then you are assigning the position, so your object will jump halfway and then if you call it again, it will have a new position to move towards, thus it doesn’t reach the random position that it got the first time around before getting assigned a new one.

@Suddoha covered the other part. You’re probably wanting to move completely before you get a new position.

1 Like

“I could imagine you want to have it moving to a target position completely before it generates a new one, but that’s just a guess”

You guessed right. I want it to move complete to the next waypoint and then only to move to the next random waypoint.
But not sure how to do it.

Try like:

class SpawnObject {
   public GameObject obj; // this could be Transform, instead..
   public Vector3 dest;
  }
// different type of list
List<SpawnObject> spawnObjects = new List<SpawnObject>();

// spawn..
// create + get position
SpawnObject so = new SpawnObject();
so.obj = newobject;
so.dest = GenerateRandomPositions(newobject);
spawnObjects.Add(so);

// Move
SpawnObject so;
for(int i = 0 ; i < spawnObjects.Count; ++i) {
   so = spawnObjects[i];
   so.obj.transform.position = Vector3.MoveTowards(so.obj.transform.position, so.dest, speed * Time.deltaTime);
   if((so.dest - so.obj.transform.position).sqrMagnitude < 0.01f)
       so.dest = GenerateRandomPositions(so.obj);
   }
1 Like