繁体   English   中英

使用 Unity 和 c# 时,如何使对象运动不那么断断续续且更加流畅?

[英]When using Unity and c#, how do I make an objects movement less choppy and more fluid?

我正在尝试制作一个可以在移动设备上播放的简单蛇游戏。 我已经达到了蛇头能够向用户滑动的方向连续移动的地步,但是蛇头的运动有点波涛汹涌,我希望它更加流畅。

我在公共课上有这个:

Vector2 dir = Vector2.right;

我的启动函数如下所示:

void Start() {

    dragDistance = Screen.height * 5 / 100; //dragDistance is 5% height of the screen
    InvokeRepeating("Move", 0.3f, 0.075f);

}

在我的更新功能里面,我有这个:

if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
            {//It's a drag
             //check if the drag is vertical or horizontal
                if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                {   //If the horizontal movement is greater than the vertical movement...
                    if ((lp.x > fp.x))  //If the movement was to the right)
                    {   //Right swipe
                        dir = Vector2.right;
                    }
                    else
                    {   //Left swipe
                        dir = -Vector2.right;
                    }
                }
                else
                {   //the vertical movement is greater than the horizontal movement
                    if (lp.y > fp.y)  //If the movement was up
                    {   //Up swipe
                        dir = Vector2.up;
                    }
                    else
                    {   //Down swipe
                        dir = -Vector2.up;
                    }
                }
            }

在更新功能之后,我有:

void Move()
{
    // Move head into new direction
    transform.Translate(dir);
}

谢谢你的帮助。

这仅仅是由于您目前的移动方式。
使用InvokeRepeating移动你的角色会非常不稳定,因为每次InvokeRepeating重复时你只是“向前”移动一定量。

你真的不应该用它来移动你的角色。 通常,您需要某种形式的刚体和物理来移动。 也许尝试这样的事情:

public Rigidbody2D rb;

void FixedUpdate() {

  rb.MovePosition((rb.position + dir) * Time.fixedDeltaTime);

}

这可能是一个非常简单的答案。 此处查看有关该方法的更多详细信息。
也可以尝试在 Unity 中查找一些有关运动的教程。

问题是你不是每一帧都在移动。 你可以通过像这样移动 update() 来解决这个问题:

transform.Translate(dir * speed * Time.deltaTime);

而不是执行 InvokeRepeating 和 Move()。

但是,如果您想让蛇像现在一样在每个方格停下来,但要从一个方格平滑地移动到另一个方格,该怎么办? 您需要在 move() 函数中保存蛇的下一个位置,然后在 Update() 中平滑地将蛇移动到那里。 它看起来像这样:

void Move()
{
    // Move head to next position
    prevSquare = transform.position;   // keep track of the previous square
                                       // so we can use lerp() later on
    nextSquare = transform.position + dir;
    progress = 0;     // progress is the percent distance between the two squares
    isMoving = true;  //we probably need isMoving so we can decide what to do if they
                      //swipe while snake is moving, I'll leave it to you to figure 
                      //out
}


update()
{
     ...

     if(isMoving)
     {
          progress = progress + time.deltaTime;
          transform.position = Vector2.Lerp(prevSquare, nextSquare, progress);
          if(progress > 1.0f)
          {
               isMoving = false;
          }
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM