0
\$\begingroup\$

I'm a beginner in Unity and pretty much copy and pasted the code but I can't figure out why the game is using the size of the sprite and not my BoxCollider2D as a reference to collide. I downscaled the Sprite from 1 to 0.32 but in game it collides as if the sprite size would be 1 even though I never used that size.

private BoxCollider2D BoxCollider;
private Vector3 moveXYZ;
private RaycastHit2D hit;

private void Start()
{
    BoxCollider = GetComponent<BoxCollider2D>();
}

private void FixedUpdate()
{
    float x = Input.GetAxisRaw("Horizontal");
    float y = Input.GetAxisRaw("Vertical");

    float aktuelleZeit = Time.deltaTime;
    moveXYZ = new Vector3(x, y, 0);

    float size = 0.32f;
    if(moveXYZ.x > 0)
    {
        
       transform.localScale = new Vector3(size, size, 0);
    }
    if (moveXYZ.x < 0)
    {
        transform.localScale = new Vector3(-size, size, 0);
    }


    hit = Physics2D.BoxCast(transform.position, BoxCollider.size, 0, new Vector2(0, moveXYZ.y), Mathf.Abs(moveXYZ.y * aktuelleZeit), LayerMask.GetMask("Collision", "Player"));
    if (hit.collider == null)
    {
        transform.Translate(0, moveXYZ.y * aktuelleZeit, 0);
    }

    hit = Physics2D.BoxCast(transform.position, BoxCollider.size, 0, new Vector2(moveXYZ.x, 0), Mathf.Abs(moveXYZ.x * aktuelleZeit), LayerMask.GetMask("Collision", "Player"));
    if (hit.collider == null)
    {
        transform.Translate(moveXYZ.x * aktuelleZeit, 0, 0);
    }

    Debug.Log(x);
    Debug.Log(y);
}
\$\endgroup\$
1
  • \$\begingroup\$ Since your code does not look like it needals dynamic scaling for your object, you could the scale to 0.32 in the editor already and afterwards adjust the box collider. It might be that your collider never gets updated and keeps the original 1 size \$\endgroup\$
    – Zibelas
    Jan 23, 2022 at 11:12

1 Answer 1

0
\$\begingroup\$

BoxCollider.size is the size of your box collider as displayed in its Inspector properties. It does not factor in the scaling you applied with these lines:

transform.localScale = new Vector3(size, size, 0);

To correct for this, you should adapt your collision query like so:

Vector2 actualSize = BoxCollider.size * size;

hit = Physics2D.BoxCast(transform.position, actualSize, 0, ...

Two other things to watch for...

  • You're applying a negative scale when moving left. This can cause some physics behaviours to misbehave, because it can turn colliders inside-out. You may want to consider using the flipX property of the SpriteRenderer instead, or putting the sprite(s) on a child object that you can scale and flip (or even squash & stretch) independent of the physics representation.

  • You're moving a physics body with Transform.Translate. This is a classic way to cause errors. It rips the object out of the control of the physics engine, teleports it, then puts it back into the physics world, breaking interpolation and overriding or disrupting normal collision handling. Consider using Rigidbody2D.MovePosition instead, or setting a velocity on your object that matches the displacement you want to achieve in the next time step.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .