Rhoran
Rhoran

Reputation: 41

How to prevent object from sliding when moving platform diagonally in Unity?

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:

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.

Upvotes: 0

Views: 343

Answers (3)

Rhoran
Rhoran

Reputation: 41

I found a solution how it works. I am moving the forks up and down with physics now.

  1. Add a Rigidbody2D to the Forks, set Type to "Dynamic", use very high mass like 100000 and set gravity scale to 0 so it won't be affected by the box with a mass of 2
  2. If you pick up the box set it to a child of the forks.
  3. If the box falls off the forks unchild it again

Moving Left/Right is the same. For Moving Up/Down use the following code:

//Forks Up/Down
    if (moveUp)
    {
        rbForks.velocity = new Vector2(0, 100 * Time.deltaTime * liftingSpeed);

    }
    else if (moveDown)
    {
        rbForks.velocity = new Vector2(0, -100 * Time.deltaTime * liftingSpeed);
    }

Upvotes: 1

Mjgholizade
Mjgholizade

Reputation: 76

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

Upvotes: 0

Anas Essouli
Anas Essouli

Reputation: 99

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

Upvotes: 0

Related Questions