Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by binaryOwl_ · Sep 25, 2019 at 09:26 PM · rotationphysics2ddirectiontopdownoverlap

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

219 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers

2D Sprite issue when rotating towards mouse click location. 2 Answers

According to Collision Change Rotation of Car 1 Answer

Moving forward based off of Rotation 3 Answers

Help with player rotation on rotating sphere 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges