0
votes

So I have the player with a Rigidbody2d and a box collider 2D with continuous collision detection and dynamic body type. The boxes the player must jump on are dynamic until they have fallen when their rigidbody gets destroyed to stop the wobbling, the box also has a collider 2d on it yet still the player can move through it. The player must move with a lerp as it must only move a certain distance. I'm pretty sure this is the problem, however, I don't know how t fix it. I have multiple raycasts which work very well except when the player lands on the box diagonally. Any help would be much appreciated.alt text

Thanks

    void update(){
    if (Input.GetAxis("Horizontal") > 0 && moveRight == true && moveRightTimer <= 0)
        {
            moveRightTimer = 0.15f;
            moveX += moveDistance;
            if(gameObject.transform.localScale.x < 0)
            {
                flipPlayer();
            }
        }
    }
    void FixedUpdate()
        {
            MovePlayer();
        }
    //Moves the player
    void MovePlayer()
    { 
        Vector2 PlayerPos = gameObject.transform.position;
        PlayerPos.x = moveX;

    GetComponent<Rigidbody2D>().MovePosition(PlayerPos + (Vector2)transform.position * Time.deltaTime);

}

enter image description here

1

1 Answers

0
votes

When setting your player's position, use either Rigidbody2D.MovePosition() or Rigidbody2D.position to set the position of your transform, rather than the transform's position property directly. Rigidbody2D.MovePosition() accounts for the interpolation setting on your rigidbody if that is something you wish to use.

Rigidbody2D.MovePosition

Rigidbody2D.position

Example:

public Rigidbody2D rigid;
public bool UseMovePosition;

void Awake()
{
    rigid = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{
    if (UseMovePosition)
        rigid.MovePosition(transform.position + transform.forward * Time.deltaTime);
    else
        rigid.position = transform.position + transform.forward * Time.deltaTime;
}

These will set the position and adhere to any collisions that would occur.