简体   繁体   English

在 unity3d 中使用 touchphase 时,rigidbody.moveposition 不起作用

[英]rigidbody.moveposition is not working while using touchphase In unity3d

I'm working on a 3d game for mobile, so I added a script that moves the player by dragging it with one finger.so I tried to make him collide with other objects with different methods but I failed to make him collide.我正在开发一款用于移动设备的 3d 游戏,因此我添加了一个脚本,该脚本通过用一根手指拖动来移动玩家。所以我试图用不同的方法让他与其他物体发生碰撞,但我未能让他发生碰撞。 so how to make him collide with objects?那么如何让他与物体发生碰撞呢?

void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Moved)
        {
            targetPosition += Vector3.right * touch.deltaPosition.x * speedmodifier;
            targetPosition += Vector3.forward * touch.deltaPosition.y * speedmodifier;
        }
    }
}
private void FixedUpdate()
{
    rb.MovePosition(targetPosition);
}

You cannot use Rigidbody.MovePosition.您不能使用 Rigidbody.MovePosition。 The Rigidbody.MovePosition is like Transform.Translate. Rigidbody.MovePosition 类似于 Transform.Translate。 It overrides any collision.它覆盖任何碰撞。 An alternative way that you could use is just adding some force (Rigidbody.AddForce) and then add a limit on the maximum speed.您可以使用的另一种方法是添加一些力(Rigidbody.AddForce),然后添加对最大速度的限制。 That worked for me.这对我有用。 Just out of curiosity, when you disable the touchphase, does it collide?只是出于好奇,当您禁用 touchphase 时,它会发生碰撞吗? I believe not but still... Anyway, the answer to your question is to just not use Rigidbody.MovePosition since it overrides any collision detection.我不相信,但仍然......无论如何,你的问题的答案是不要使用 Rigidbody.MovePosition 因为它会覆盖任何碰撞检测。

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

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