c# - 如何在动画播放时延迟统一运动

标签 c# unity3d wait

我在 Unity 2018.4 中很难延迟我的角色的移动,我怀疑这更多的只是我的代码的问题而不是 unity。

编辑: 我希望能够按住右箭头键,让角色不动 415 毫秒,然后能够移动 580 毫秒,之后他不能移动 350 毫秒(让动画完成播放)

我尝试在 IE 分子中使用倒计时等待然后调用我的移动函数,但这导致我的角色在动画播放后没有移动。

//What makes the character move, works fine if executed on its own in update
private void ExecuteMovement(float dir)
    {
        currentPosition = transform.position;
        currentPosition.x += dir * Time.deltaTime * speed;
        transform.position = currentPosition;
    }

//Trying to use waitforseconds to delay the execution while the animation plays
private IEnumerator Movement(float dir)
    {
        yield return new WaitForSeconds(0.5f);

        ExecuteMovement(dir);
    }

void Update()
    {
        if (0 > Input.GetAxis("Horizontal"))
        {
            //This is to flip image
            transform.eulerAngles = new Vector3(0, 180, 0);

            //starts animation
            animator.SetBool("isSkipping", true);

            //calls the movement function with the direction to move in
            Movement(Input.GetAxis("Horizontal"));

        }

        else if (0 < Input.GetAxis("Horizontal"))
        {
            //This is to flip image
            transform.eulerAngles = new Vector3(0, 0, 0);

            //starts animation
            animator.SetBool("isSkipping", true);

            //calls the movement function with the direction to move in
            Movement(Input.GetAxis("Horizontal"));

        }
    }

我很乐意尝试任何其他方法来延迟角色的移动。动画显示角色冲上去跳跃,然后跳跃。我只想在它在空中时移动,这大约是动画的半秒。

最佳答案

您正在尝试像调用方法一样调用您的 IEnumerator


相反,您必须将其作为 Coroutine 启动。使用 StartCoroutine

StartCoroutine(Movement(Input.GetAxis("Horizontal")));

看到你的编辑

I want to be able to hold down the right arrow key, have the character not move for 415ms, then be able to move for 580ms, after which he cannot move for 350ms (to let the animation finish playing)

到现在为止,既然您想要继续移动但仍然完全控制速度,我会将其保留在 Update 中并仅使用协程来控制标志。协程仍然更易于读/写和维护。 (现在它也允许空中方向切换..不知道你是否想要这个):

// control the ability to move with a simple flag
// instead of various states
public bool canMove;

// check if a jumping is already being animated/executed 
// -> disallow a new jumping animation until it is finished
private bool canJump = true;

private void Update()
{
    // avoid repeated API calls
    var input = Input.GetAxis("Horizontal");

    if (input < 0)
    {
        //This is to flip image
        transform.eulerAngles = new Vector3(0, 180, 0);

        if(canJump)
        {
            //starts the movement
            StartCoroutine(Jump());
        }
    }
    else if (input > 0)
    {
        //This is to flip image
        transform.eulerAngles = new Vector3(0, 0, 0);

        if(canJump)
        {
            //starts the movement
            StartCoroutine(Jump());
        }
    }

    // if can not move do nothing else
    if(!canMove) return;

    // execute the movement
    transform.position += Vector3.right * input * speed * Time.deltaTime;
}

private IEnumerator Jump()
{
    // disable jumping
    canJump = false;

    // disable move during animation
    canMove = false;

    //starts animation
    animator.SetBool("isSkipping", true);

    // not move for 415ms
    yield return new WaitForSeconds(0.415f);

    // enable move
    canMove = true;

    // be able to move for 580ms
    yield return new WaitForSeconds(0.580f);

    // disable move
    canMove = false;

    // cannot move for 350ms during end of animation
    yield return new WaitForSeconds(0.350f);

    // enable move again
    canMove = true;

    // re-enable jumping
    canJump = true;
}

关于c# - 如何在动画播放时延迟统一运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57299904/

相关文章:

c# - 在具有相同命名空间的多个项目的解决方案中找不到命名空间

c# - 将 Unity 设置从二进制转换为文本

c# - 不卡住 Thread.Sleep 的替代品,用于在任务中等待

java - notifyAll 是否在不检查条件的情况下从循环中删除等待?

c# - 在 ASP.NET Core 中到达 Controller 之前拦截不良请求

c# - Linq 中字符(字符串)编码的数字与数字的比较

c# - NancyFX 将日期时间反序列化为本地类型

c# - 为什么我在 Unity3D 的场景中从一个空的 GameObject 获得了 30 个绘制调用?

unity3d - 如何通过代码访问 Unity 的 TextMesh Pro Dropdown 组件?

wait - 如何减少 Selenium2Library Robot Framework 中的等待时间