【问题标题】:Rigidbody isn't colliding with objects when using rigibody.MovePosition使用刚体.MovePosition 时刚体不与物体发生碰撞
【发布时间】:2020-07-22 16:47:12
【问题描述】:

我试图通过触摸移动我的播放器并使其发生碰撞,所以我编写了这个脚本,但问题是我的播放器没有与其他对象发生碰撞,那么如何让它发生碰撞,那么如何解决这个问题?

[SerializeField] private Rigidbody rb;
private Vector3 targetPosition;

private void Start()
{
    targetPosition = transform.position;
    if (!rb)rb = GetComponent<Rigidbody>();
    // since this rigibody is going to be moved via code not Physics it should be kinemtic
    rb.isKinematic = true;
    // in order to smooth the movement
    rb.interpolation = RigidbodyInterpolation.Interpolate;
}

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

【问题讨论】:

  • 确保您的对撞机不会太薄,然后重试。
  • @MathewHD 对撞机就像我的播放器

标签: c# unity3d 3d rigid-bodies


【解决方案1】:

您可以将MovePosition() 与非运动刚体一起使用,但它会像 transform.position=newPosition 一样工作并将对象传送到新位置(而不是执行平滑过渡)。此外,您可以通过修改其变换组件从脚本中移动运动刚体对象,但它不会像非运动刚体那样响应碰撞和力。

因此,如果您希望刚体正确地受到物理影响,您需要使用 AddForce() 方法并将您的刚体设置为 Dynamic

示例:

player.AddForce(new Vector2(speed, 0), ForceMode2D.Impulse);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多