- Home /
Question by
carpediem23 · Aug 24, 2012 at 05:58 PM ·
coroutinemovesmoothienumerator
Smooth moving object
hi guys,
how can i be working simple move with ienumerator ? i maked something for it, like this:
//This numerator do to changing plyer's direction
IEnumerator TakeMe(int paramDirection)
{
yield return new WaitForEndOfFrame();
if(_canChangePosition)
{
Debug.Log("Take me : " + paramDirection + " " + "direction");
Transform _tempTransform = null;
switch (paramDirection)
{
case 0:
_tempTransform = playersLeft;
break;
case 1:
_tempTransform = playersRight;
break;
default:
break;
}
while (Vector3.Distance(player.transform.position, _tempTransform.position) > 0)
{
//player.transform.position += Vector3.right * _moveSpeed * myPlayerSpeed;
//player.transform.position = Vector3.Lerp(player.transform.position, _tempTransform.position, Vector3.Distance(player.transform.position, _tempTransform.position) / Time.deltaTime * 0.25f);
player.transform.position += Vector3.right * _moveSpeed * myPlayerSpeed;
}
_canChangePosition = false;
}
}
private void ChoosePosition(int paramPositionInfo, int paramWantedDirection)
{
int tempDirection = _nullValue;
if(_canChangePosition)
{
if(paramPositionInfo == (int)LandStatus.OnCenter)
{
StartCoroutine("TakeMe", paramWantedDirection);
}
else if(paramPositionInfo == (int)LandStatus.OnLeft && paramWantedDirection != (int)Directions.Left)
{
StartCoroutine("TakeMe", paramWantedDirection);
}
else if(paramPositionInfo == (int)LandStatus.OnRight && paramWantedDirection != (int)Directions.Right)
{
StartCoroutine("TakeMe", paramWantedDirection);
}
else
_canChangePosition = false;
}
}
private void CheckUpControls()
{
if(_canMove)
{
if (Input.GetKeyDown(KeyCode.A))
{
_targetDirectionTransform = playersLeft;
_playersLand = FindPlayersLand();
Debug.Log("Player's position number is :" + _playersLand);
ChoosePosition(_playersLand, (int)Directions.Left);
Debug.Log("User click LEFT");
}
else if (Input.GetKeyDown(KeyCode.D))
{
_targetDirectionTransform = playersRight;
_playersLand = FindPlayersLand();
Debug.Log("Player's position number is :" + _playersLand);
ChoosePosition(_playersLand, (int)Directions.Right);
Debug.Log("User click RIGHT");
}
}
}
void Update ()
{
_distance = Vector3.Distance(_startPointTransform.position, _endPointTransform.position);
//10 meter per second
_moveSpeed = Time.deltaTime * speedFactor;
_playersTransform = _player.transform;
//Check Is user click to Left or Right
CheckUpControls();
}
but i cant be moving object smoothly how can i do with this ?
Comment
Answer by lancerfour · Aug 26, 2012 at 10:22 AM
In your `TakeMe()` coroutine, try moving the `yield return new WaitForEndOfFrame();` into the end of the `while` loop.
Adding it to the loop should show the smoothed movement, updating it as it goes.
Let me know if it worked :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613080126im_/https://answers.unity.com/themes/thub/images/avi.jpg)