1
votes

I have two GameObjects with a box collider. One is the player, moved with a fixed velocity towards the other. The collision stops the player (the Poo character ;D)

But they overlap, you can see it here: enter image description here

I don't know why this is happening. Colliding with the top or bottom works just fine...The same effect happens from the left side. The green block has just a collider, no RigidBody.

Gif: enter image description here

Another Gif, with MovePosition() ... Colliding fromt the top works, but "reentering" stops the character. Why?!: enter image description here

  1. GIF, moving up and down is okay, left and right at the top of the blocks slows him down. Weird... enter image description here

Movement Script:

public class PlayerController : MonoBehaviour
{
    public float Speed = 10f;

    private Rigidbody2D rb2D;

    private Vector2 DirectionLeft;
    private Vector2 DirectionRight;
    private Vector2 DirectionUp;
    private Vector2 DirectionDown;

    private Vector2 CurrentDirection;

    // Use this for initialization
    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();

        DirectionLeft = new Vector2(Speed*-1, 0);
        DirectionRight = new Vector2(Speed, 0);
        DirectionUp = new Vector2(0, Speed * -1);
        DirectionDown = new Vector2(0, Speed);

        CurrentDirection = DirectionLeft;
    }

    void SetAnimationDirection()
    {
        Vector3 scale = transform.localScale;

        if (CurrentDirection == DirectionLeft)
            scale.x = 1;
        else
            scale.x = -1;

        transform.localScale = scale;
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        if (moveHorizontal > 0)
            CurrentDirection = DirectionRight;
        if (moveHorizontal < 0)
            CurrentDirection = DirectionLeft;
        if (moveVertical < 0)
            CurrentDirection = DirectionUp;
        if (moveVertical > 0)
            CurrentDirection = DirectionDown;

        Debug.Log(CurrentDirection);
        SetAnimationDirection();

        rb2D.velocity = CurrentDirection;
    }
1
Hi, the problem is because you are modifying the transform.localScale in the SetAnimationDirection function. This is being done every frame or physics frame. It would be good to only execute transform.localScale = scale When you change the move direction instead of doing it every time.....Don't you think so?Programmer
This is why I said you have to resize the collider to match after changing the radius. Go to Scene tab then resize each collider. It is easy to do and resizing a collider will not resize the sprites.There is no need to open new question for this. Just Google how to resize a collider in Unity.Programmer
You are welcome. It doesn't have to be 0.9. Any number that will do it is fine. Happy coding!Programmer
Now that you have arrived at a solution to the problem, one of you (DoubleVoid or @Programmer) needs to post it in the answer box below. Answers do not belong in comments.Cody Gray
@DoubleVoid You can put what we discussed that worked for you as an answer. That's fine with me.Programmer

1 Answers

1
votes

The solution to the first problem (overlapping):

I used transform.localScale = scale to change the direction of the sprite on every FixedUpdate() and also forgot to update the factor of the scale, since I added a new extension, which resized my object by 1 more Unity unit.

The other problem (getting stuck at the edge) is solved adding setting the Edge Radius of the Collider 2D to something below 1.0f (e.g 0.09f) and also resize the bounding box. This will prevent the object from getting stuck at the edges, because the bounding box edges are now rounded.