简体   繁体   English

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

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

I'm trying to make a simple snake game on unity that can be played on a mobile device.我正在尝试制作一个可以在移动设备上播放的简单蛇游戏。 I got to the point where the snakes head is able to move continuously in the direction that the user swiped in, but the movement of the snake's head is kind of choppy and I'd like it to be more fluid.我已经达到了蛇头能够向用户滑动的方向连续移动的地步,但是蛇头的运动有点波涛汹涌,我希望它更加流畅。

I have this in the public class:我在公共课上有这个:

Vector2 dir = Vector2.right;

My start function looks like this:我的启动函数如下所示:

void Start() {

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

}

Inside of my Update function I have this:在我的更新功能里面,我有这个:

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;
                    }
                }
            }

And after the Update function I have:在更新功能之后,我有:

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

Thanks for your help.谢谢你的帮助。

This is simply due to how you are currently moving.这仅仅是由于您目前的移动方式。
Using InvokeRepeating to move your character will be super choppy as you are just moving "forward" by a certain amount every time InvokeRepeating repeats.使用InvokeRepeating移动你的角色会非常不稳定,因为每次InvokeRepeating重复时你只是“向前”移动一定量。

You really shouldn't be using that to move your character.你真的不应该用它来移动你的角色。 Typically you want to you some form of Rigidbody and physics to move.通常,您需要某种形式的刚体和物理来移动。 Perhaps try something like this :也许尝试这样的事情:

public Rigidbody2D rb;

void FixedUpdate() {

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

}

That could be a very simple answer.这可能是一个非常简单的答案。 Check out some more details on that method here .此处查看有关该方法的更多详细信息。
Also just try looking up some tutorials on movement in Unity.也可以尝试在 Unity 中查找一些有关运动的教程。

The problem is you aren't moving every frame.问题是你不是每一帧都在移动。 You could fix this by moving in the update() like so:你可以通过像这样移动 update() 来解决这个问题:

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

Instead of doing InvokeRepeating and Move().而不是执行 InvokeRepeating 和 Move()。

But what if you want to make the snake stop at each square like it does now, but move smoothly from square to square?但是,如果您想让蛇像现在一样在每个方格停下来,但要从一个方格平滑地移动到另一个方格,该怎么办? You would need to save the snake's next position in your move() function and then smoothly move the snake there in Update().您需要在 move() 函数中保存蛇的下一个位置,然后在 Update() 中平滑地将蛇移动到那里。 It would look something like this:它看起来像这样:

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