Home > Software engineering >  How to prevent object from sliding when moving platform diagonally in Unity?
How to prevent object from sliding when moving platform diagonally in Unity?

Time:12-06

I am trying to implement a simple fork lift truck in Unity. Moving left, right, up and down is working fine: Box stays in place

Moving left/right and up/down at the same time (box moving diagonally) the box is sliding off the forks: Box is sliding off

Does anyone have an idea?

What i already tried to do:

  • When picking up the box, make it a child of the fork lift truck
  • Adding Physics Material 2D with high friction to forks and box
  • Set the x-velocity of the box to the x-velocity of the fork lift truck
  • Decreasing Movement- and Lifting speed of the fork lift truck
  • Decreasing fixed timestemp in Project Settings
  • Increasing Mass and Gravity Scale of the Box

Fork lift truck and the box both have a rigidbody2D attached with Body Type Dynamic and Collision Detection Continuous.

Currently iam moving the fork lift truck with the following code:

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

The box should not slide off when moving e.g. left and up and the same time.

CodePudding user response:

when moving try to parent the box to the forklift's front thing and disable the rigidbody, something like this

if (isMoving)
      {
          box.trasform.parent = Lifter; 
          box.GetComponent<Rigidbody2D>().isKinematic = true;
      }
      else 
      {
          box.trasform.parent = null; 
          box.GetComponent<Rigidbody2D>().isKinematic = false;
      }

CodePudding user response:

Just add a fixed Joint 2D component to the box and connect it to the player whenever you want.

If it doesn't work, add two fixed joints

  • Related