Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jcs2213 · Feb 24, 2015 at 10:53 AM · renderhitboxfightingrectangle

Drawing Rectangles in 2D for hitboxes: fighting game

This is about to drive me insane. My goal is to draw a 2D rectangle with the same dimensions as the hitbox I want to create in Unity. Because my Box Collider 2D isn't visible, I'm trying to make a visual aid to see where it actually is in the world.

Unfortunately, drawing this rectangle has been a far bigger headache than I would have hoped. I've searched for many examples as to how to do this - I've looked at GL.DrawTexture and GL.Box, but this will not work because drawing this rectangle is an Animation Event. In short, I am calling a function during a particular frame that is dynamically creating a hitbox, and then I am trying to get rid of it a few frames later. The rectangle I am drawing is only relevant for these frames, and I'm not doing anything in OnGUI.

Right now, I have:

     Texture2D tex = new Texture2D (1, 1);
     tex.SetPixel (0, 0, Color.black);
     tex.Apply ();
     Rect pos = new Rect (transform.position.x, transform.position.y, 10, 10);
     Sprite newSprite = Sprite.Create (tex, pos, new Vector2 (0.5f, 0.5f), 128);
     GameObject obj = new GameObject ();
     obj.name = "Hitbox";
     obj.AddComponent<SpriteRenderer> ();
     SpriteRenderer spr = obj.GetComponent<SpriteRenderer> ();
     spr.sprite = newSprite;

I am trying to draw a little black box in front of the player during a walk animation. Not only does this not work, I can't get rid of the rectangle when I need to. The rectangle doesn't even appear where it should - it's always at 0,0.

Any help would be appreciated; this problem is making me lose my mind.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RexSpaceman · Feb 24, 2015 at 10:53 PM

You can use Debug.DrawLine to draw 4 lines at any point from any method. Downside, is that they are only visible in editor.

So based off your existing code, you could do something like this:

 //Top Line
 Debug.DrawLine(new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, 0),
 new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, 0), Color.black);
 //Right Line
 Debug.DrawLine(new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, 0),
 new Vector3(transform.position.x + 0.5f, transform.position.y - 0.5f, 0), Color.black);
 //Bottom Line
 Debug.DrawLine(new Vector3(transform.position.x + 0.5f, transform.position.y - 0.5f, 0),
 new Vector3(transform.position.x - 0.5f, transform.position.y - 0.5f, 0), Color.black);
 //Left Line
 Debug.DrawLine(new Vector3(transform.position.x - 0.5f, transform.position.y - 0.5f, 0),
 new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, 0), Color.black);

Another option is you could use a OnDrawGizmos() and do something like (drawBlackBox would be a class variable or something similar):

 void OnDrawDizmos()
 {
     if (drawBlackBox)
     {
         Gizmos.color = Color.black;
         Gizmos.DrawWireCube(transform.position.x, new Vector3(1f, 1f, 1f));
     }
 }

The downside of Gizmos is they're only visible in the editor as well.

If you want it to work anywhere, and it doesn't need to be too complex, then you could consider using a Quad Primitive and appropriate texture/material and make it a prefab. Instantiate it when the game starts or character spawns or whatever, store a reference to it and then just turn it on/off/update when necessary.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A Fighting Games Hit boxes 1 Answer

Stop Moving at a Certain Distance from a Hit Box 1 Answer

Hit detection for Fighting Games? 1 Answer

using openGL to render simple line 1 Answer

Another Java to C# Conversion Question!! 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