So, I am currently working on a script to interact with objects from a top-down perspective, both my player character and interactable object are squares.
My player is generally able to interact with the object, except every now and again (seemingly at random) the left (and only the left) side of the object becomes impossible to interact with, due to some kind of thin invisible hitbox(?) appearing there. This only happens upon pressing play, this hitbox never appears while the code is running.
This hitbox is completely impenetrable, EXCEPT if the player moves diagonally into it, at which point the player moves past it to the object’s “actual” hitbox, and it doesn’t return until I press play again.
There doesn’t appear to be anything wrong with my box colliders in either the player or object, and after doing some Debug.Log - ing, it appears that the problem is caused by the thin hitbox blocking the collision that should occur (and not a problem with my interaction code.)
I’m using C#, here’s my interaction script with some of the collision code inside it
protected virtual void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
player = GameObject.Find("Player");
thePlayerScript = player.GetComponent<Player>();
playerTransform = player.GetComponent<Transform>();
interAction= InputSystem.actions.FindAction("Interact");
}
protected virtual void Update()
{
//collision work
boxCollider.OverlapCollider(filter, hits);
for (int i = 0; i < hits.Length; i++)
{
if (hits[i] == null)
continue;
//This section only happens if there's a collision.
//If player is to the right and facing left:
if ((playerTransform.position.x <= transform.position.x + 0.4) && thePlayerScript.playerDirection == "Left" && interAction.IsPressed() && (playerTransform.position.y >= transform.position.y - 0.1) && (playerTransform.position.y <= transform.position.y + 0.1))
{
Interact();
}
//If player is to the left and facing right:
if ((playerTransform.position.x >= transform.position.x - 0.4) && thePlayerScript.playerDirection == "Right" && interAction.IsPressed() && (playerTransform.position.y >= transform.position.y - 0.1) && (playerTransform.position.y <= transform.position.y + 0.1))
{
Interact();
}
//If player is to the top and facing down:
if ((playerTransform.position.y <= transform.position.y + 0.4) && thePlayerScript.playerDirection == "Down" && interAction.IsPressed() && (playerTransform.position.x >= transform.position.x - 0.1) && (playerTransform.position.x <= transform.position.x + 0.1))
{
Interact();
}
//If player is to the bottom and facing up:
if ((playerTransform.position.y >= transform.position.y - 0.4) && thePlayerScript.playerDirection == "Up" && interAction.IsPressed() && (playerTransform.position.x >= transform.position.x - 0.1) && (playerTransform.position.x <= transform.position.x + 0.1))
{
Interact();
}
//The array is not cleaned up every time, so we do it ourself
hits[i] = null;
}
}
Then here’s the player script’s movement and collision code:
hity = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
if (hity.collider == null)
{
//makes player actually move (y)
transform.Translate(0, moveDelta.y * Time.deltaTime, 0);
}
hitx = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
if (hitx.collider == null)
{
//makes player actually move (x)
transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);
}
(There is no blocking layer related to this problem, and the object is on the actor layer.)
Does anyone have an idea about how I would fix this issue?
My apologies if I used incorrect terminology or failed to give enough information, I am still very new to C# programming and Unity.