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 JellyCakes · Mar 05, 2011 at 05:33 PM · animationforceattackmelee

Melee attacking, using force and health values

Hi there

I only know the sheer basics of scripting in C# so i don't know how to approach this.

Basically, When the left mouse button is pressed, it plays a melee animation.

Now what i want to happen is that if the distance of an enemy is lower than lets say 10, for the health value of the enemy to decrease and the enemy knocked back from force.

Not only that, i want all this to happen at a certain point in the animation. The animation is a Spear swinging back, then lunging foward for the attack. So basically what i want is that the health value decreases once the animation reaches a certain frame or point in time.

Much Appreciated

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 MortenK84 · Mar 05, 2011 at 07:56 PM

You could split up your animation, into lets say "swingAttackPart1" and "swingAttackPart2". swingAttackPart1 should go to the frame where you want the health of the enemy to decrease / knock enemy back.

In code, you'd then start playing swingAttackPart1, wait for it to finish, then once it's finished, you'd be at the correct point in time. Then you'd just decrease the health, and simultaneously play swingAttackpart2 as well as possibly a "gettingHit" animation for the enemy.

It would look something like this:

GameObject enemyGameObject=GameObject.Find("enemy");

 If (distance <= 10) {
 yield return StartCoroutine(swingPart1); //"Yield return" makes sure code execution doesn't continue until the coroutine is finished.
 gameObject.animation.Play("swingPart2"); //Now that swingPart1 has finished, we can play the "gettingHit" animation of enemy, and decrease enemies health.
 enemyGameObject.animation.Play("gettingHit");
 DecreaseHealthOfEnemy(); //Function that decreases enemy health
 }

The coroutine would look like

Ienumerator swingPart1() {
gameObject.animation.Play("swingPart1"); //Start animation
while (gameObject.animation.IsPlaying()) { //Loop that checks if the animation is still running. If it is, "yield return null", skips a frame, and run the loop again)
yield return null;
}
}

As an alternative, you could keep the swingAttack in one animation, and then make a counter in the above coroutine that skips a frame until you've reached the frame number you want. Like so:

Ienumerator swingAnimation() {
gameObject.animation.Play("swing"); //Start animation
int frameToStopAt=30;
for (int i=0; i < frameToStopAt; i++) { 
yield return null;
}
}

In this case, you'd remove gameObject.animation.Play("swingPart2"); in the parent method. The code ain't tested, but it should give you the general idea.

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

No one has followed this question yet.

Related Questions

Play attack animation on mousebutton 1 Answer

please help with my melee attack animation 1 Answer

Animations Combo 0 Answers

Not identifying the collider? 2 Answers

2 Animations At once 0 Answers


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