OverlapBoxAll rotation
Hi, I am making a topdown 2D rpg game and I'm trying to implement a melee combat system. I have decided to use OverlapBoxAll which activates when the player presses the space button. I have a 4 directional movement system and the overlapbox needs to change its position if the player is looking north, south, west or east. I have managed to make it work on a boxcollider2D but I can't seem to figure it out if I use OverlapBoxAll.
This is the Swing function. Everything works if the player is looking south.
private void Swing()
{
if (Input.GetKey(KeyCode.Space))
{
ChangeRotation();
Collider2D[] targetsToDamage = Physics2D.OverlapBoxAll(targetPos.position + offset, new Vector2(boxSizeX, boxSizeY), degrees, whatIsTarget);
for (int i = 0; i < targetsToDamage.Length; i++)
{
targetsToDamage[i].GetComponent<Resource>().TakeDamage(damage);
}
timeBetweenAttacks = startTimeBetweenAttacks;
}
}
This is the function which changes the degrees variable. My intention was to change the degrees of the variable and then add it to the angle parameter of the OverlapBoxAll function. I really don't understand why it doesn't work because it works if I use it on a BoxCollider2D. Basically I just change the Z axis of it to 0, 90, 180 or 270 and boxcollider changes its position.
I have noticed that changing the angle parameter of the overlapboxall function doesn't change anything.
private void ChangeRotation()
{
if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingNorth())
{
degrees = 180f;
}
else if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingWest())
{
degrees = 270f;
}
else if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingEast())
{
degrees = 90;
}
else if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingSouth())
{
degrees = 0;
}
}
I am really, really stuck and need help in understanding the overlapboxall function.
Also it would be very helpful if I knew how to visualize overlapboxall. I am drawing the box with Gizmos currently but don't know how to rotate it because there's no argument with which I can do that.
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(targetPos.position + offset, new Vector2(boxSizeX, boxSizeY));
}
Answer by binaryOwl_ · Sep 25, 2019 at 11:28 PM
I figured it out. It turns out changing the angle does work but it wasn't enough. If I only changed the angle the OverlapBox was placed below the player game object which meant I had to change the offset as well.
if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingNorth())
{
offset = new Vector3(0, 0.25f, 0);
degrees = 180f;
}
else if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingWest())
{
offset = new Vector3(-0.25f, 0, 0);
degrees = 270f;
}
else if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingEast())
{
offset = new Vector3(0.25f, 0, 0);
degrees = 90;
}
else if (transform.parent.GetComponent<CheckPlayerDirection>().IsMovingSouth())
{
offset = new Vector3(0, -0.25f, 0);
degrees = 0;
}
I added the particular offset for each case (north,south,west,east) and then the overlapbox was in the correct position. It was a very easy thing to solve once I had the proper tools for visualisation. A user that goes by the name DMGregory on StackExchange gave the solution for the last problem with which I was easily able to see where overlapbox was. You don't have to use gizmos to rotate the overlapbox, just use this custom function made by him.
void DebugDrawBox( Vector2 point, Vector2 size, float angle, Color color, float duration) {
var orientation = Quaternion.Euler(0, 0, angle);
// Basis vectors, half the size in each direction from the center.
Vector2 right = orientation * Vector2.right * size.x/2f;
Vector2 up = orientation * Vector2.up * size.y/2f;
// Four box corners.
var topLeft = point + up - right;
var topRight = point + up + right;
var bottomRight = point - up + right;
var bottomLeft = point - up - right;
// Now we've reduced the problem to drawing lines.
Debug.DrawLine(topLeft, topRight, color, duration);
Debug.DrawLine(topRight, bottomRight, color, duration);
Debug.DrawLine(bottomRight, bottomLeft, color, duration);
Debug.DrawLine(bottomLeft, topLeft, color, duration);
}
Anyways, hope this will help someone someday. :)
Answer by unity_iSgM0XuibzHT8Q · Jan 22, 2021 at 12:05 PM
Hi! I was stuck for days with the OverlapBoxAll function for an attack orientation (since I have 8 attack directions for a top down game), and none of the tutorials on YouTube nor in other websites solved the problem. Your solution was the only one that really helped me! Thanks man!