【问题标题】:How to prevent object from sliding when moving platform diagonally in Unity?在Unity中对角移动平台时如何防止物体滑动?
【发布时间】:2022-12-20 08:24:12
【问题描述】:

我正在尝试在 Unity 中实现一个简单的叉车。向左、向右、向上和向下移动工作正常: Box stays in place

同时向左/向右和向上/向下移动(盒子沿对角线移动)盒子从叉子上滑落: Box is sliding off

有人有想法吗?

我已经尝试过的事情:

  • 拿起箱子时,将其设为叉车的孩子
  • 为叉子和盒子添加具有高摩擦力的二维物理材料
  • 将箱子的 x 速度设置为叉车的 x 速度
  • 降低叉车的移动和提升速度
  • 减少项目设置中的固定时间戳
  • 增加盒子的质量和重力比例

叉车和箱子都有一个 rigidbody2D,带有 Body Type Dynamic 和 Collision Detection Continuous。

目前我正在使用以下代码移动叉车:

private void FixedUpdate()
    {
        //Moving Left/Right
        if (moveRight)
        {
            timeElapsedDeceleration = 0;
            rb.velocity = new Vector2(Mathf.Lerp(rb.velocity.x, drivingSpeed, timeElapsedAcceleration / 2), rb.velocity.y);
            timeElapsedAcceleration += Time.fixedDeltaTime;
        }
        else if (moveLeft)
        {
            timeElapsedDeceleration = 0;
            rb.velocity = new Vector2(Mathf.Lerp(rb.velocity.x, -drivingSpeed, timeElapsedAcceleration / 2), rb.velocity.y);
            timeElapsedAcceleration += Time.fixedDeltaTime;
        }
        else
        {
            timeElapsedAcceleration = 0;
            rb.velocity = new Vector2(Mathf.Lerp(rb.velocity.x, 0, timeElapsedDeceleration / 2), rb.velocity.y);
            timeElapsedDeceleration += Time.fixedDeltaTime;
        }

        //Lifting
        if (moveForksUp && forks.transform.localPosition.y <= maxLiftHeight)
        {
            forks.transform.localPosition = new Vector2(forks.transform.localPosition.x, forks.transform.localPosition.y + liftingSpeed * Time.fixedDeltaTime);
            liftableMast.transform.localPosition = new Vector2(liftableMast.transform.localPosition.x, liftableMast.transform.localPosition.y + liftingSpeed / 1.5f * Time.fixedDeltaTime);
        }
        else if (moveForksDown && forks.transform.localPosition.y >= minLiftHeight)
        {
            forks.transform.localPosition = new Vector2(forks.transform.localPosition.x, forks.transform.localPosition.y - liftingSpeed * Time.fixedDeltaTime);
            liftableMast.transform.localPosition = new Vector2(liftableMast.transform.localPosition.x, liftableMast.transform.localPosition.y - liftingSpeed / 1.5f * Time.fixedDeltaTime);
        }
    }

箱子在移动时不应滑落,例如左右同时。

【问题讨论】:

    标签: c# unity3d physics


    【解决方案1】:

    您可以使用物理学来防止箱子从叉车上滑落。您可以向盒子添加一个刚体,并设置其约束,使其只能沿 x 轴或 y 轴移动,但不能同时沿两者移动。

    这是如何实现的示例:

    public class BoxController : MonoBehaviour
    {
      // The speed at which the box moves
      public float speed = 1.0f;
    
      // The rigidbody component attached to the box
      private Rigidbody rb;
    
      // Start is called before the first frame update
      void Start()
      {
        rb = GetComponent<Rigidbody>();
    
        // Set the constraints on the rigidbody so that it
        // can only move along the x-axis or y-axis, but not both
        rb.constraints = RigidbodyConstraints.FreezePositionZ |
                         RigidbodyConstraints.FreezeRotationX |
                         RigidbodyConstraints.FreezeRotationY;
      }
    
      // Update is called once per frame
      void Update()
      {
        // Get the horizontal and vertical input from the user
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
    
        // Apply the input as a force to the rigidbody
        rb.AddForce(new Vector3(horizontalInput, verticalInput, 0) * speed);
      }
    }
    

    【讨论】:

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