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