Hello! Again thanks so much for your help. I am still learning and practicing and I must admit that I can’t advance without a little help. Or maybe alot, I am not sure.
Let me start explaining my actuals problem, but first a look for what I am trying to do, more or less:
Now let’s start.
I did a static StartPosition and EndPosition just for not be totally stopped. And managed to do the movement for all my GameObject in the Update, but it isn’t very smoother.
After it I did it in with a couritine thanks to other user that help me before.
IEnumerator ObjectMover(GameObject obj)
{
float progress = 0;
while(progress < 1f)
{
progress += Time.deltaTime * speedItems;
obj.transform.position = Vector3.Lerp(startPosition, finishPosition, Mathf.Clamp(progress, 0, 1));
yield return null; //Wait until next frame to process again
}
Destroy(obj);
}
This code, effectively, move my GameObjects smoother, but with two problems: They “teleports” to a startPosition instead their first random position AND they arrives all at same time to the FinishPosition.
So, thinking from my novice knowledge, that they all arrive at same time is “progress” var faults AND that they “teleports” to startPosition is “Lerp” faults because it have a “startPosition”.
So I did the next:
IEnumerator ObjectMover(GameObject obj, Vector3 originalObjPosition)
{
float fullDistance = Vector3.Distance (startPosition.transform.position, finishPosition.transform.position);
float relativeDistance = Vector3.Distance (originalObjPosition, finishPosition.transform.position);
float progress = relativeDistance / fullDistance;
while(progress < 0.95f)
{
progress += Time.deltaTime * speedItems;
obj.transform.position = Vector3.Lerp(originalObjPosition, finishPosition, Mathf.Clamp(progress, 0, 1));
yield return null; //Wait until next frame to process again
}
Destroy(obj);
}
First I did a little code for check the Distance between FullDistance and relativeDistance. If we divide both we get the progress between 0 and 1.
And changed the Lerp for use the obj Original Position.
I changed too the while because, sometimes, they just keep so many time to dissapear, dunno why.
Well, the result is… strange. They all teleports very close to FinishPosition, but finally they don’t disappear at same time.
If I use the same code but use again the StartPosition in the lerp the result is that all of my GameObjects teleport to the middle, making a perfect line/queue.
At this point, dunno what I could try.
The next question is the direction. For now my objects are moving exactly from A to B, but I want really they move in a direction, with a constant speed.
And when they arrive a certain point must be destroyed. Maybe this point should be the edge of a imaginarie circle (I am using already radius for generate my items around a point. I guess I could manage it).
But anyway the point is… How to move GameObjects in a direction?
Just continuing with the direction. I really don’t want the StartPosition and the FinishPosition (they are for now two empty GameObjects placed where I want them exactly).
The goal is make a combo-box with cardinals points. (Already done)
So per example. If the user select “North” then the GameObjects should move to “South” (I know there aren’t “really” cardinals points, they are just how we see it).
Once time selected the North the program should calculate the Start Position as a Vector3 at a point in the edge of that imaginarie circle and the South just the oposite side.
And then it should calculate the direction from where my GameObjects where start to move and where will finish.
I tryed do it already, but the distance between points aren’t correct. I am missing something here:
void PlacingTheStartAndFinishPositionInTheirFinalPositions(string coordinate)
{
switch (coordinate)
{
case "West":
startPositionVector = new Vector3 (radius, middlePoint.transform.position.y, 0);
finishPositionVector = new Vector3 (radius*-1, middlePoint.transform.position.y, 0);
break;
case "NorthWest":
startPositionVector = new Vector3 (radius, middlePoint.transform.position.y, radius);
finishPositionVector = new Vector3 (radius*-1, middlePoint.transform.position.y, radius*-1);
break;
case "North":
startPositionVector = new Vector3 (0, middlePoint.transform.position.y, radius);
finishPositionVector = new Vector3 (0, middlePoint.transform.position.y, radius*-1);
break;
case "NorthEast":
startPositionVector = new Vector3 (radius*-1, middlePoint.transform.position.y, radius);
finishPositionVector = new Vector3 (radius, middlePoint.transform.position.y, radius*-1);
break;
case "East":
startPositionVector = new Vector3 (radius*-1, middlePoint.transform.position.y, 0);
finishPositionVector = new Vector3 (radius, middlePoint.transform.position.y, 0);
break;
case "SouthEast":
startPositionVector = new Vector3 (radius*-1, middlePoint.transform.position.y, radius*-1);
finishPositionVector = new Vector3 (radius, middlePoint.transform.position.y, radius);
break;
case "South":
startPositionVector = new Vector3 (0, middlePoint.transform.position.y, radius*-1);
finishPositionVector = new Vector3 (radius, middlePoint.transform.position.y, radius);
break;
case "SouthWest":
startPositionVector = new Vector3 (radius*-1, middlePoint.transform.position.y, radius*-1);
finishPositionVector = new Vector3 (radius, middlePoint.transform.position.y, radius);
break;
}
Debug.Log("Distance" + Vector3.Distance(startPositionVector,finishPositionVector));
}
Of course this code isn’t correct. The Distance should be always the same if it is done correctly (diametre). I should work more with it, but for now I let this here waiting for better ideas.
Btw, the Y axis isn’t important at all, they are predefinide with other variable (middlePoint).
And I guess that is all. Thanks so much for your help. I should go sleep a bit ^^.
