首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一:触点延迟太高了

统一:触点延迟太高了
EN

Stack Overflow用户
提问于 2020-07-25 21:16:14
回答 1查看 306关注 0票数 0

我想做一个游戏,在那里你可以扔物品在2D通过拖放。这很好,但跟踪抓住他们是非常糟糕的。如果我在上半个地方碰它们,跟踪就不起作用,甚至几乎不起作用。

还有其他方法来防止这种情况发生吗?

代码语言:javascript
复制
public class DropsBehaviour : MonoBehaviour {
    //Declaring Variables
    public int force = 10;
    float maxSpeedPreset = 5;
    float maxSpeed = 5;
    float MaxThrowSpeed = 8;

    GameManager gm;
    Rigidbody rig;
    Camera cam;

    bool selected = false;
    bool once = false;
    void Start() {
        gm = GameManager.FindMe();
        rig = GetComponent<Rigidbody>();
        cam = Camera.main;
    }

    void Update() {
        if (selected) {
            rig.AddForce((cam.ScreenToWorldPoint(Input.mousePosition) - transform.position) * force);

            rig.velocity = Vector3.zero;
        } else {
            if (!once) {
                maxSpeed = maxSpeedPreset * gm.gameSpeed;
                rig.velocity = new Vector3(Mathf.Clamp(rig.velocity.x, -maxSpeed, maxSpeed), Mathf.Clamp(rig.velocity.y, -maxSpeed, maxSpeed), Mathf.Clamp(rig.velocity.z, -maxSpeed, maxSpeed));
            } else {
                maxSpeed = maxSpeedPreset;
                rig.velocity = new Vector3(Mathf.Clamp(rig.velocity.x, -MaxThrowSpeed, MaxThrowSpeed), Mathf.Clamp(rig.velocity.y, -MaxThrowSpeed, MaxThrowSpeed), Mathf.Clamp(rig.velocity.z, -MaxThrowSpeed, MaxThrowSpeed));
            }
        }
    }

    void OnMouseDown() {
        if (tag != "NoInteract") {
            selected = true;
            once = true;
        }
    }

    void OnMouseUp() {
        selected = false;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-25 22:40:21

我在您发布的代码片段中看到了一些可能导致口吃的问题。

首先,如果您要用2D制作一些东西,建议使用Rigidbody2D和Collider2D。

然后,你不应该用手编辑刚体的速度,如医生所述。这就是增加力量的目的。在更新任何与物理有关的内容时,您应该使用FixedUpdate,而不是更新。

我猜你也忘了在第一遍上写“一次=假”?真的不确定这件事。

我按照前面提到的要点编辑了您的脚本,同时保持刚体3D。我没有测试它,但希望它能纠正你正在经历的奇怪的拖曳行为!(请注意,我删除了GameManager部分,因为我不知道它做了什么)

代码语言:javascript
复制
public class DropsBehaviour : MonoBehaviour
{
    public int force = 10;

    private const float MaxSpeedPreset = 5;
    private float _maxSpeed = 5;
    private const float MaxThrowSpeed = 8;

    private Rigidbody _rig;
    private Camera _cam;

    private bool _selected;
    private bool _once;
    private Vector3 _targetVelocity;

    private void Start()
    {
        _rig = GetComponent<Rigidbody>();
        _cam = Camera.main;
    }

    private void Update()
    {
        if (_selected) return;

        if (!_once)
        {
            _maxSpeed = MaxSpeedPreset * 10;
            _targetVelocity = new Vector3(Mathf.Clamp(_rig.velocity.x, -_maxSpeed, _maxSpeed),
                Mathf.Clamp(_rig.velocity.y, -_maxSpeed, _maxSpeed),
                Mathf.Clamp(_rig.velocity.z, -_maxSpeed, _maxSpeed));
        }
        else
        {
            _maxSpeed = MaxSpeedPreset;
            _targetVelocity = new Vector3(Mathf.Clamp(_rig.velocity.x, -MaxThrowSpeed, MaxThrowSpeed),
                Mathf.Clamp(_rig.velocity.y, -MaxThrowSpeed, MaxThrowSpeed),
                Mathf.Clamp(_rig.velocity.z, -MaxThrowSpeed, MaxThrowSpeed));
            _once = false; //Dunno if it's intended or not ?
        }
    }

    private void FixedUpdate()
    {
        if (_selected)
        {
            _rig.AddForce((_cam.ScreenToWorldPoint(Input.mousePosition) - transform.position) * force);
            _rig.velocity = Vector3.zero;
            return;
        }

        var velocity = _rig.velocity;
        var velocityChange = _targetVelocity - velocity;
        _rig.AddForce(velocityChange, ForceMode.VelocityChange);
    }

    private void OnMouseDown()
    {
        if (CompareTag("NoInteract")) return;

        _selected = true;
        _once = true;
    }

    private void OnMouseUp()
    {
        _selected = false;
    }
}

顺便提一句,我个人不喜欢标签,我会用LayerMask代替,因为它有点与物理有关。我可能也会用Raycast来代替OnMouse事件,这样你就可以更好地控制物理方面发生的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63093649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档