C# 每x次移动一个游戏对象x次

C# 每x次移动一个游戏对象x次,c#,unity3d,coroutine,C#,Unity3d,Coroutine,我有一个游戏对象,我希望它每1.5秒向下移动0.5秒(类似于“移动冷却”)。(作为游戏的附加信息是2d)希望我能得到一个答案:D private float _speed=2.0f; void Update() { StartCoroutine(Move()); } IEnumerator Move() { float timeElapsed = 0.0f; while(timeElapsed<=0.5f) { transform.T

我有一个游戏对象,我希望它每1.5秒向下移动0.5秒(类似于“移动冷却”)。(作为游戏的附加信息是2d)希望我能得到一个答案:D

private float _speed=2.0f;

void Update()
{
    StartCoroutine(Move());

}

IEnumerator Move()
{
    float timeElapsed = 0.0f;

    while(timeElapsed<=0.5f)
    {
        transform.Translate(Vector3.down * _speed * Time.deltaTime);
        yield return null;
       
    }
    yield return new WaitForSeconds(1.5f);
    _speed = 0.0f;

}
private float\u速度=2.0f;
无效更新()
{
start例程(Move());
}
IEnumerator Move()
{
浮动时间经过=0.0f;

虽然(timeappeased虽然我对Unity不太熟悉,但类似的方法可能会奏效:

private float _speed=2.0f;
private float timeSinceLastMovement = 0f;

void Update()
{
    timeSinceLastMovement += Time.deltaTime;
    if (timeSinceLastMovement >= 1.5)
    {
         StartCoroutine(Move());
         timeSinceLastMovement = 0;
    }
    
}

IEnumerator Move()
{

    float timeElapsed = 0.0f;
    while(timeElapsed<=0.5f)
    {
        transform.Translate(Vector3.down * _speed * Time.deltaTime);
        timeElapsed += Time.deltaTime;
        yield return null;
    }
}

private float\u速度=2.0f;
私有浮动时间自上次移动=0f;
无效更新()
{
TimesInclastMovement+=Time.deltaTime;
如果(TimesInclastMovement>=1.5)
{
start例程(Move());
TimesInclastMovement=0;
}
}
IEnumerator Move()
{
浮动时间经过=0.0f;

当(timeElapsed@MrSausage如果答案解决了您的问题,请接受答案,否则此问题仍将无法回答。