- Home /
Write IEnumerator for Movement
I want to write an IEnumerator to move at the desire distance at specified time.
I have tried to write the code for this but this is running a different way.
float moveDistance=1f;
float moveSpeed=5f;
float elapsedDistance = 0f;
while (elapsedDistance <= moveDistance)
{
elapsedDistance += Time.deltaTime * moveSpeed;
Vector3 cubeLocalPosition = transform.localPosition;
cubeLocalPosition.y += Time.deltaTime * moveDistance;
transform.localPosition = cubeLocalPosition;
yield return null;
}
Through this code, Object can't able to travel 1 unit distance. Please help me to correct this code.
Answer by Hellium · May 07, 2020 at 08:26 PM
Vector3 targetPosition = transform.position + Vector3.up * moveDistance;
float moveSpeed = 5f;
while (targetPosition != transform.position)
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
yield return null;
}
It does not give me many details about the current behaviour, the expected one and the actual issue you are facing.
Yes, you are right it's a part of the work. Basically I want to create a cube path wave in which cubes in sequence will up and down.
So this is just half code though which I want to move my each cube upside after this I want to move my code downside too at which it actually exists.
Now let me introduce you the whole code step by step: Through the below code, I have tried to create a wave effect by lifting the block step by step by the time. And I have managed each column block separately for my game future requirements.
public void SpawnPathCubesWaveWithColor(Color waveColor)
{
inFirstColumnObstacleFound = inSecondColumnObstacleFound = inThirdColumnObstacleFound = false;
StartCoroutine(StartSpawningWaveWithColor(waveColor));
}
IEnumerator StartSpawningWaveWithColor(Color waveColor)
{
for (int i = 0; i < totalCubeColumns; i++)
{
if (!inFirstColumnObstacleFound)
firstColumnBlocks[i].GetComponent<PathBlock>().ActivatePathBlock(waveColor);
if (!inSecondColumnObstacleFound)
secondColumnBlocks[i].GetComponent<PathBlock>().ActivatePathBlock(waveColor);
if (!inThirdColumnObstacleFound)
thirdColumnBlocks[i].GetComponent<PathBlock>().ActivatePathBlock(waveColor);
yield return waitForFractionOfSecond;
}
}
Then after the actual cube moves up and down code:
public void ActivatePathBlock(Color waveColor)
{
my$$anonymous$$eshRenderer.material.color = waveColor;
StartCoroutine($$anonymous$$ovePathBlockUpDown());
}
IEnumerator $$anonymous$$ovePathBlockUpDown()
{
Vector3 targetPosition = transform.localPosition + Vector3.up * moveDistance;
// move cube upside
while (((transform.localPosition - targetPosition).sqr$$anonymous$$agnitude) > 0f)
//while (targetPosition != transform.localPosition)
{
transform.localPosition = Vector3.$$anonymous$$oveTowards(transform.localPosition, targetPosition, moveSpeed * Time.deltaTime);
yield return null;
}
// move cube downside
targetPosition = initialLocalPosition;
while (((transform.localPosition - targetPosition).sqr$$anonymous$$agnitude) > 0f)
{
transform.localPosition = Vector3.$$anonymous$$oveTowards(transform.localPosition, targetPosition, moveSpeed * Time.deltaTime);
yield return null;
}
}
}
Your answer
Follow this Question
Related Questions
Need localPosition.x in world units. 2 Answers
Local distance movement 0 Answers
geting the closest object from a array 2 Answers
making an object stop responding with an object at a certain distance away 1 Answer
Detonator and it's volume threshold 0 Answers