Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ramsdal · Nov 06, 2015 at 03:43 PM · collidertriggercombatanimation eventsmelee attack

Melee Combat Collision

Hi All

I am trying to get some melee combat working; in this case I have a character (3rd person) with various attack animations. One or two of these animations seems to be too fast for the system, here is what I have done.

The character has a sword equipped (with a collider on the blade). When the character attacks an attack animation is triggered, when the animation reaches a certain point an AnimationEvent is fired (this event just sets a bool used to determine if the collision should be handled or not - used to avoid attacking an enemy when walking into one for example).

This works well, most of the time! But what I am experiencing is that once in a while the sword passes through the target without hitting, but then it hits on the return trip (by that I mean that the animation pulls the sword back to the ready stance after swinging, and this drags the blade (and hence the collider) through the target and triggers the hit).

So in short it seems that the sword passes through the enemy collider sometimes too fast (it is a fast attack). This is also something I have seen others mention that perhaps the animation is so fast that the position of the blade skips such a distance that at one frame it is “to one side of the enemy collider” and the next frame it is “on the other side of the enemy collider”?

In any case, I have tried to slow the animation and increase the enemy collider size, both seems to do the trick, however I would like to hear if there is a better solution for this “issue” and if it is indeed happening because the attack animation is too “fast paced”.

Also would I be better off dropping the collider approach and instead make an AnimationEvent at the point where the sword is supposed to “hit” and then do a RayCast in this event to check if an enemy is close enough and handle the hit here.

I hope it makes sense to someone!!!

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

Answer by iDerpCake · Nov 07, 2015 at 07:34 AM

This might sound confusing but I think I know a fix. When you hit the attack key or button make it so it enables a game object with a box collider in front of the player. Then make it so when the box collider collides with the enemy then send a message dealing the damage. I hope this worked if not try using it as a trigger instead. Hoped this helped

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 rhbrr5hrfgdfgw · Nov 07, 2015 at 01:23 PM

As you press the attack button send a raycast (just for example with a range of 2) and if it hits the enemy (lets say it has a "Enemy" tag then deal the damage, something like that:

 var hit : RaycastHit;
 
 void Update() {
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        if (Physics.Raycast(aaaaaa, fwd, hit, 2) && hit.transform.tag == "Enemy") {
             
             print("There is something in front of the object!");
         }
     }
Comment
Add comment · Show 2 · 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 Ramsdal · Nov 09, 2015 at 12:35 PM 0
Share

$$anonymous$$y problem is that I would like the impact to play a particle effect, and that should be played when the sword hits the target - which is a while after the attack button is clicked.

But perhaps I could do what you say from the hilt of the sword and out. However I suspect that I would still encounter the same issue in that case, namely that the raycast will occur one frame before the target, and then next raycast will be sent after the sword has passed the target.

Again I am back to two options as I can see it? Either trigger the particle effect and hit using a Animation Event (placed in the animation where the sword is straight out from the target) OR expand the collider.

The main issue is if this is actually happening because the frame skips such a distance that it passes the collider before the next frame. If that is the case then it should be something that has happened often to others with bullets I would guess (at least bullets done using colliders).

avatar image rhbrr5hrfgdfgw · Nov 11, 2015 at 04:09 PM 0
Share

To be more clear, this is what you have to do: At the scene you make an empty gameobject and place it inside the player as a child, at about the middle or the top of the sword(you will have to check what works the best for you). So on click make a raycast that goes out of your gameobject. now as you click when you check this code:

         if (Physics.Raycast(aaaaaa, fwd, hit, 2) && hit.transform.tag == "Enemy") {
              
              print("There is something in front of the object!");
          }

It says that the raycast hitted something, so after a the animation is over just instantiate an GameObject variable(the particles). To check when the animation is over check how much time the animation actually lasts and after that time instantiate. the code should look something like that:

     var hit : RaycastHit;
     var theNewEmptyGameObject : GameObject;
     
     void SendRayCast(){
             if (Physics.Raycast(theNewEmptyGameObject , theNewEmptyGameObject .forward, hit, 2) && hit.transform.tag == "Enemy") {
                  yield WaitForSeconds(YourAnimationTime);
                  print("Now you can instantiate the particles");
              }
     }
      
      void Update() {
     if(Input.Get$$anonymous$$ouse.......)
                 SendRayCast();
          }
 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

Ball activating triggers even with a script 0 Answers

How can I attach a colldier that moves with the enemies sword in an attack animation? 0 Answers

How do you detect which collider is triggered when you have multiple? 2 Answers

Trigger and Collider Issues 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