遵循Unity-snake教程,但控件有延迟

时间:2016-03-13 04:16:34

标签: c# unity3d controls delay

我正在关注制作团结蛇游戏的教程。问题是,当我按下一个键并且蛇实际上转动时,有一个很小但非常明显的延迟。延迟是不一致的,有时候我可以完成U形转弯,有时它会在U形转弯完成之前跳过几个街区。这是一个大问题,因为它是一个快速的游戏,所有关于计时。

感谢您的帮助!这是代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Snake : MonoBehaviour {
    int frame = 0;
    Vector2 dir = Vector2.right;

    void Start()
    {// repeats Move function
        InvokeRepeating("Move", 0.1f, 0.05f);
    }


    // Update is called once per frame
    void Update () {
        frame += 1;
        // Move in a new Direction?
        if (Input.GetKey(KeyCode.RightArrow))
            dir = Vector2.right;
        else if (Input.GetKey(KeyCode.DownArrow))
            dir = -Vector2.up;    // '-up' means 'down'
        else if (Input.GetKey(KeyCode.LeftArrow))
            dir = -Vector2.right; // '-right' means 'left'
        else if (Input.GetKey(KeyCode.UpArrow))
            dir = Vector2.up;
    }

        void Move() {
        transform.Translate(dir);
    }
}

1 个答案:

答案 0 :(得分:1)

发现了代码的少数问题。对于人眼来说, 。您正在调用更新蛇的方向每0.05 秒的功能,这可能导致延迟

我添加snakeSpeed作为变量来控制蛇的速度。您可以更改编辑器中的值或不同级别的代码。 更低值,更慢蛇。蛇的值更高更快。我将InvokeRepeating替换为Coroutine,这更适合您正在做的事情。由于您提供了完整的代码,因此您可以获得完整的功能代码。

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    public class Snake : MonoBehaviour
    {
        Vector2 dir;
        public float snakeSpeed = 0.06f; //To control the speed of the snake
        bool continueMoving = false;

        int frame = 0;

        void Start()
        {
            dir = Vector2.right * snakeSpeed;
            StartCoroutine(MoveSnake());
        }


        // Update is called once per frame
        void Update()
        {
            frame += 1;
            // Move in a new Direction?
            if (Input.GetKey(KeyCode.RightArrow))
                dir = Vector2.right * snakeSpeed;
            else if (Input.GetKey(KeyCode.DownArrow))
                dir = -Vector2.up * snakeSpeed;    // '-up' means 'down'
            else if (Input.GetKey(KeyCode.LeftArrow))
                dir = -Vector2.right * snakeSpeed; // '-right' means 'left'
            else if (Input.GetKey(KeyCode.UpArrow))
                dir = Vector2.up * snakeSpeed;
        }

        //Call to start moving
        IEnumerator MoveSnake()
        {
            if (continueMoving)
            {
                yield break; //Make sure there is one instance of this function running
            }
            continueMoving = true;

            //Continue moving nonstop until continueMoving is false or  stopSnake() function is called.
            while (continueMoving)
            {
                transform.Translate(dir);

                yield return null;
            }
        }

        //Call to Stop Moving Snake
        void stopSnake()
        {
            continueMoving = false;
        }
    }

我认为您不需要额外的函数调用或调用重复,但我将其包含在您需要的教程中。使用较少代码实际执行此操作的正确方法如下:

   Vector2 dir;
    public float snakeSpeed = 0.06f; //To control the speed of the snake
    int frame = 0;

    void Start()
    {
        dir = Vector2.right * snakeSpeed;
    }


    // Update is called once per frame
    void Update()
    {
        frame += 1;
        // Move in a new Direction?
        if (Input.GetKey(KeyCode.RightArrow))
            dir = Vector2.right * snakeSpeed;
        else if (Input.GetKey(KeyCode.DownArrow))
            dir = -Vector2.up * snakeSpeed;    // '-up' means 'down'
        else if (Input.GetKey(KeyCode.LeftArrow))
            dir = -Vector2.right * snakeSpeed; // '-right' means 'left'
        else if (Input.GetKey(KeyCode.UpArrow))
            dir = Vector2.up * snakeSpeed;

        transform.Translate(dir);
    }