Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
6
Question by Carthage · Dec 31, 2012 at 11:51 PM · animationblenderframehitbox

How can I do frame by frame hitbox control for a 2D fighting game character?

I have created a 3D model and some basic walk, run, idle, and attack animations in blender. I would like to import these into a 2D unity fighting game and be able to control the hitboxes of the attacks on a frame by frame basis. Is this possible? Is there a tutorial or something showing how this is done in unity?

Comment
Add comment · Show 2
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 Pranxta · Jan 13, 2014 at 07:15 AM 0
Share

been looking for a way to achieve the same result for a while with no luck yet.

avatar image lyie · Jan 18, 2016 at 02:23 AM 0
Share

Just copy and pasted into Untiy 5 (Im sorry Ill edit it once I get better at scripting) and the colliders are there but the hitboxes don't clear. one is always hanging around. Any suggestions?

1 Reply

· Add your reply
  • Sort: 
avatar image
16

Answer by Lovelock · Oct 12, 2014 at 06:12 AM

This isn't the prettiest solution but it gets the job done. I'm using 2D Sprites but it can be applied to 3D GameObjects as well.

This system has three parts:

  • A HitboxManager script to swap hitboxes when needed

  • Animation Events that tell the HitboxManager when to draw hitboxes

  • A reference hitbox for each frame that needs a hitbox

Before we get started, here's an example of a HitboxManager script:

 using UnityEngine;
 using System.Collections;
 
 public class HeroHitBoxManager : MonoBehaviour {
 
     // Set these in the editor
     public PolygonCollider2D frame2;
     public PolygonCollider2D frame3;
 
     // Used for organization
     private PolygonCollider2D[] colliders;
 
     // Collider on this game object
     private PolygonCollider2D localCollider;
 
     // We say box, but we're still using polygons.
     public enum hitBoxes
     {
         frame2Box,
         frame3Box,
         clear // special case to remove all boxes
     }
     
     void Start()
     {
         // Set up an array so our script can more easily set up the hit boxes
         colliders = new PolygonCollider2D[]{frame2, frame3};
 
         // Create a polygon collider
         localCollider = gameObject.AddComponent<PolygonCollider2D>();
         localCollider.isTrigger = true; // Set as a trigger so it doesn't collide with our environment
         localCollider.pathCount = 0; // Clear auto-generated polygons
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         Debug.Log("Collider hit something!");
     }
 
     public void setHitBox(hitBoxes val)
     {
         if(val != hitBoxes.clear)
         {
             localCollider.SetPath(0, colliders[(int)val].GetPath(0));
             return;
         }
         localCollider.pathCount = 0;
     }
 }
 

Basic Workflow

For this example we'll be using Sprites and PolygonCollider2Ds.

Drag the sprites that need hit boxes into the scene. Attach a PolygonCollider2D component to each sprite. Click the Edit Collider button and fit the polygon to wherever you want the hit box for that frame. Make sure to do that for each sprite.

Sprites with PolygonCollider2D

Next, attach the HitboxManager script to your actual character (Sprite) GameObject. This is the GameObject that will need to be doing the actual collision detection and animations.

This script will hold references to all of the colliders we just hand made. It already has room for two--currently named frame2 and frame3.

Create a public PolygonCollider2D variable for each sprite you customized earlier. Go back to the inspector and drag each sprite into the corresponding slot in the HitboxManager component. Make sure to also update the hitBoxes enum with each frame, and fill out the colliders array in Start().

Now the HitboxManager can use the collider info from each sprite. Our last step is to tell the manager when and what collider to use.

Animation Events

Note that the HitboxManager includes a setHitBox() function. This is what we will call from the animator to display or clear hit boxes as needed.

The character you're working on should already have the HitboxManager script attached, and this should also be the GameObject that handles the animating. Select that GameObject and open up the Animation editor.

From the clip dropdown, select the animation that needs the hit boxes we've been working on.

To add an event, right click above a frame and select "Add Animation Event". Choose setHitBox() from the function dropdown. Select the value that corresponds to the frame (and thus hit box) you want to enable with this animation event at this frame.

Go ahead and add a setHitBox() clear event somewhere near the end of your animation. This just resets the collider so your hit boxes don't stay forever.

That's it!

Test your scene out and watch the collider update in sync with your animation. To see the collider, make sure to have the GameObject selected while playing, and enable Gizmos in the top right of the Game view.

Want to see OnTriggerEnter2D() fire when your collider hits something? Create another game object that has a 2D collider on it, set Is Trigger to true, add a Rigidbody2D component and set Is Kinematic to true. Your trigger events should now fire whenever you hit this GameObject!

Cleanup: You can also disable all the sprites that you have in the scene you created earlier. Don't delete them though or the link to their collider info will be gone.

Frame by Frame Hitboxes

Enjoy these placeholder graphics as well.


spriteswithcolliders.png (18.7 kB)
spriteswithcolliders02.png (43.6 kB)
Comment
Add comment · Show 10 · 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 Noodles the Cat · Jan 15, 2015 at 01:22 AM 0
Share

This really helped out a big deal, was actually one of the biggest obstacles I was dealing with thank you very much for this.

avatar image richyrich · Jan 15, 2015 at 01:25 AM 0
Share

@Noodles the Cat - when thanking or commenting, please add a comment. I've converted your answer to comment this time.

avatar image Memnok · Feb 01, 2015 at 05:37 PM 0
Share

Could this be customized to accept box and circle colliders as well?

avatar image Lovelock · Apr 07, 2015 at 10:04 PM 0
Share

Absolutely. You could easily change each occurrence of PolygonCollider2D to BoxCollider2D or CircleCollider2D--as long as your collision type is consistent. This script could be modified to accept multiple types at once but would need some clever modification first. And witchcraft.

avatar image Cherno · Jun 22, 2015 at 05:33 PM 1
Share

You could use tags for that, or layers.

Show more comments

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

17 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

Related Questions

Select frame for Animation 1 Answer

A quick question about animation frames 1 Answer

Need help with animation rewind 1 Answer

Play sound on anim frame 1 Answer

Remember animation frames when reloading model 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