Translating an Object to a Random Endpoint,Moving an Object to a random endpoint
I've been following tutorials and looking at past posts, i sill could not figure how to move an object from one end to a random end (ex: left to a random point on the right).
Any other solutions and extra tips are appreciated aswell! Thanks!
{
float timer;
float timeToMove;
public float speed;
float yPos;
public Vector3 desiredPos;
void Start()
{
timer = 0;
timeToMove = 3;
yPos = Random.Range(-4.5f, 4.5f);
desiredPos = new Vector3(transform.position.x, yPos, transform.position.z);
}
void Update()
{
timer += Time.deltaTime;
if (timer >= timeToMove)
{
// I copied this, I do not know what this does
transform.position = Vector3.Lerp(desiredPos, transform.position, Time.deltaTime * speed);
if (Vector3.Distance(transform.position, desiredPos) <= 0.01f)
{
yPos = Random.Range(-4.5f, 4.5f);
desiredPos = new Vector3(transform.position.x, yPos, transform.position.z);
timer = 0.0f;
}
}
}
}
,Ive been following tutorials and looking at other posts, i could not figure out how to translate an object to one position to a random end position.
{ float timer; float timeToMove; public float speed; float yPos; public Vector3 desiredPos; void Start() { timer = 0; timeToMove = 3; yPos = Random.Range(-4.5f, 4.5f); desiredPos = new Vector3(transform.position.x, yPos, transform.position.z); } void Update() { timer += Time.deltaTime; if (timer >= timeToMove) { // I copied this, I do not know what this does transform.position = Vector3.Lerp(desiredPos, transform.position, Time.deltaTime * speed); if (Vector3.Distance(transform.position, desiredPos) <= 0.01f) { yPos = Random.Range(-4.5f, 4.5f); desiredPos = new Vector3(transform.position.x, yPos, transform.position.z); timer = 0.0f; } } } }
the end as in destination. For example, I want to move something on coordinates (-4 ,0) to coordinates (4, random y coord). This is a 2d game if it helps. Thanks
Your answer
Follow this Question
Related Questions
How to Move an Object to a Random Position 0 Answers
Position of empty game objects 1 Answer
Car Controls without physics. 1 Answer
Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer
Vector3.Lerp not working properly, making the player bounce around 2 Answers