Reputation: 17
Found how to do it but why it's moving so fast ? How can i make it move much slower ? Tried to change the speed to 0.3f instead 20 but still too fast.
Maybe not using Vector3.Lerp but Translate ? I want to be able to control the speed from very slow even 0 not moving at all to very fast like it is now.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
public GameObject objectToPlace;
public Vector3 newObjectsSize = new Vector3(5,5,5);
private int currentObjects;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
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()
{
if (currentObjects != numberOfObjects)
{
GameObject newObject = (GameObject)Instantiate(objectToPlace);//(GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity);
newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
newObject.transform.localPosition = GenerateRandomPositions(newObject);
objects.Add(newObject);
currentObjects += 1;
}
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position, GenerateRandomPositions(objects[0]), (Mathf.Sin(20f * Time.time)));
}
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);
}
}
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.
Upvotes: 0
Views: 256
Reputation: 63
Hen, add a public float for controlling the speed, then use Time.deltaTime in your code. Use these two lines
public float speed;
and
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position,
GenerateRandomPositions(objects[0]), (Mathf.Sin(speed * Time.deltaTime)));
Good luck.
Upvotes: 1