Unity 2d 输入延迟 [英] Unity 2d Input lag

查看:83
本文介绍了Unity 2d 输入延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个物理性质类似于星露谷的 2d 开放世界游戏,但由于某种原因,即使我删除了所有其他脚本和动画,角色的输入也有很多延迟.

I'm making a 2d open world game with physics similar to that of stardew valley, but for some reason there's a lot of input lag on the character even when I remove all the other scripts and animations.

我什至尝试移除大部分精灵,例如房屋等,但它仍然有很多延迟,不是帧率延迟,只是玩家运动.

I even tried removing most of the sprites such as houses and whatnot but it still has a lot of lag, not framerate lag, just the player movement.

我将尝试解释:如果你只是轻轻敲击键盘,玩家移动和停止就好了,但是如果你按住按钮然后放手(大约 5 秒会产生最大的延迟,之后不会变得更糟那个),在你松开按键后,播放器会继续移动一点.

I will attempt to explain: If you just lightly tap the keyboard the player moves and stops just fine, but if you hold down the button then let go (for about 5s gives the most lag, doesn't get any worse after that), the player keeps moving a little after you let go of the key.

代码如下:

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

public class MovementControls : MonoBehaviour {
    public float speed;
    private Vector2 MoveSpeed;
    private Rigidbody2D Player;

    void Start() {
        Player = GetComponent<Rigidbody2D>();
    }
    void Update() {
        Vector2 PlayerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        MoveSpeed = PlayerInput.normalized * speed;
    }
    void FixedUpdate() {
        Player.MovePosition(Player.position + MoveSpeed * Time.fixedDeltaTime);
    }
}

有人知道如何解决这个问题吗?

Anyone know how to fix this?

推荐答案

以下是为遇到上述问题的其他人解决此问题的方法:

Here's what fixed the problem to anyone else with said problem:

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

public class MovementControls : MonoBehaviour {
    public float speed; //Change the player's speed
    private Vector2 MoveSpeed; //This is what the player's speed will be set to after some math
    private Rigidbody2D Player; //The player

    void Start() {
        Player = GetComponent<Rigidbody2D>(); //Find the player GameObject
    }
    void Update() {
        Vector2 PlayerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); //Get input
        MoveSpeed = PlayerInput * speed; //Math
        Player.MovePosition(Player.position + MoveSpeed * Time.fixedDeltaTime); //Move the player
    }
}

这篇关于Unity 2d 输入延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆