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 Robwhitlow3 · Jan 17, 2012 at 05:41 AM · playerattack

Multiple player attacks

Hi I am trying to find a way to make it so my player attack can have multiple attacks that each do different damage. Here is the script I have. using UnityEngine; using System.Collections;

public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;

 // Use this for initialization
 void Start () {
     attackTimer = 0;
     coolDown = 1.5f;
 
 }
 
 // Update is called once per frame
 void Update () {
     if(attackTimer > 0)
         attackTimer -= Time.deltaTime;
     if(attackTimer < 0)
         attackTimer = 0;
     
     if(Input.GetKeyUp(KeyCode.Mouse0)) {
         if(attackTimer == 0) {
         Attack();
             attackTimer = coolDown;
             
         }
     }
 
 }
 
 private void Attack () {
     float distance = Vector3.Distance(target.transform.position, transform.position);
     
     Vector3 dir = (target.transform.position - transform.position).normalized;
     float direction = Vector3.Dot(dir, transform.forward);
     
     Debug.Log(direction);
     
     if(distance < 3.5f){
         if(direction > 0) {
         
     EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
     eh.AddjustCurrentHealth(-15);
         }
     }
 }

}

Comment
Add comment · Show 1
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 doomprodigy · Jan 17, 2012 at 06:33 AM 1
Share

Specify what you want. Attacks that do different damage is easy. Do you want a combo system pressing the same button in succession doing attacks or do you want different buttons corresponding to different attacks. Your question is not very specific

4 Replies

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

Answer by doomprodigy · Jan 17, 2012 at 11:06 PM

 private int attackNum = 0;
 private bool attacking = false;

 
 void Update (){
 if (Input.GetButtonDown("Attack")){
 if (attacking == false){
  if (attackNum == 0){
  StartCoroutine("AttackOne");
  attackNum += 1;
  }
  if (attackNum == 1){
  StartCoroutine("AttackTwo");
  //from here onwards you would either add another digit to attackNum and put another if statement asking if it is attackNum 2 or you would reset attackNum to 0 again then repeat sort of deal`
     }
    }
   }
 
 IEnumerator AttackOne () {
 attacking = true;
 //Do your attacking script here, you will also want to Wait for the animation to stop playing or use a yield return new WaitForSeconds(somenumber); then you turn attacking false then you can press it again and call a different attack script with different animations damage etc etc etc.
 attacking = false;
 }
 //Repeat the IEnumerator function with the different name, damage and animation.

I also suggest adding a timer to start after the first attack that once it reaches a certain number it resets attackNum to 0 every swing you will need to reset the timer and start again. Attack One, start timer, attack two reset timer and start it, wait too long stop timer and reset it and reset attackNum to 0.

Peace,

Comment
Add comment · Show 1 · 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 Skirvir · Jun 23, 2019 at 06:59 AM 0
Share

Referencing where you script "attackNum += 1", would I say the same thing for each subsequent line or would you I put in "attackNum += 2, 3, etc" for each additional attack? Sorry if the question is stupid, I'm new to all of this and just playing around :D

avatar image
0

Answer by Robwhitlow3 · Jan 17, 2012 at 02:12 PM

Sorry, yes I want to make it so when I push 1 it does one troupe of attack, and when I push 2 it does another, and so on

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 Pilot · Jan 18, 2012 at 03:09 AM

Another possible approach would be to use animation events.

Set up additional input keys to correspond with your 2nd, 3rd etc attack animations and then using the animation window, add an animation event to the frame when the character is supposed to be hitting something. Have this event call a script with the specific amount of damage you want it to do.

Watch this really awesome tutorial if you haven't used animation events before, they're really easy and great to use: Unity Animation Events

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 Robwhitlow3 · Jan 30, 2012 at 10:00 PM

I ended up doing this as my final script

using UnityEngine; using System.Collections;

public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown; public float attackTimer2; public float coolDown2;

 // Use this for initialization
 void Start () {
     attackTimer = 0;
     coolDown = 1.5f;
     
     attackTimer2 = 0;
     coolDown2 = 2.0f;
     
 
 }
 
 // Update is called once per frame
 void Update () {
     if(attackTimer > 0)
         attackTimer -= Time.deltaTime;
     if(attackTimer < 0)
         attackTimer = 0;
     
     if(Input.GetKeyUp(KeyCode.Alpha1)) {
         if(attackTimer == 0) {
         Attack();
             attackTimer = coolDown;
             
         }
     }
     
     if(attackTimer2 > 0)
         attackTimer2 -= Time.deltaTime;
     if(attackTimer2 < 0)
         attackTimer2 = 0;
             
             if(Input.GetKeyUp(KeyCode.Alpha2)){
                 if(attackTimer2 ==0) {
                     Attack2();
                     attackTimer2 = coolDown2;
         }
     }
 
 }
 
 private void Attack () {
     float distance = Vector3.Distance(target.transform.position, transform.position);
     
     Vector3 dir = (target.transform.position - transform.position).normalized;
     float direction = Vector3.Dot(dir, transform.forward);
     
     Debug.Log(direction);
     
     if(distance < 3.5f){
         if(direction > 0) {
         
     EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
     eh.AddjustCurrentHealth(-5);
         }
     }
 }
 
 private void Attack2 () {
     float distance = Vector3.Distance(target.transform.position, transform.position);
     
     Vector3 dir = (target.transform.position - transform.position).normalized;
     float direction = Vector3.Dot(dir, transform.forward);
     
     Debug.Log(direction);
     
     if(distance < 5.5f){
         if(direction > 0) {
         
     EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
     eh.AddjustCurrentHealth(-10);
         }
     }
 }

}

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

7 People are following this question.

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

Related Questions

How to make my player not lose health when attacking the enemy? 2 Answers

Seting Fire Up as a attack 1 Answer

How do I make it so that I can attack more than 1 enemy? 1 Answer

Why Zombie clones dont take Player's HP? 0 Answers

auto select enemys 3 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