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 CrispyCarry · Feb 16, 2014 at 08:18 PM · tagswhyno

Adding another attack

I am trying to add a combo attack and I googled an example script but I don't know how to add another attack to it. I understand the code in it, but I don't get how to add another attack after "attack1". I spent about a day studying the code and tried multiple things but couldn't figure it out. Some help would be nice but not a spoon feed please!

 public var attackTime : float[]; // store each length of each attack in order (in the inspector)
 //public var attackAnimations : AnimationClip[]; // store a reference to the animations, then use the same index 'attackType'
  
 public var waitTime : float = 0.3; // how long player has to hit LMB after last attack has finished
  
 private var attackTimer : float = 0.0; // timer variable
 private var attackType : int = -1; // reference to what attack the combo is up to (-1 is none, 0+ is the array index for attackTime )
  
 enum attack { Idle, Attacking, Waiting } // different states while the combo inputs are being checked
 var attackState : attack; // current attack input state (public for viewing in the Inspector)
  
 var totalChainedHits : int = 0; // total Chained Hits without missing a LMB press (public for viewing in the Inspector)
  
  
 function Start() 
 {
     // set defaults
     attackType = -1;
     attackState = attack.Idle;
     // tell animation to play Idle
     animation.CrossFade("Idle");
     totalChainedHits = 0;
  
     // You could set up the attackTime array here (and make it private)
     
     attackTime = new float[3];
     attackTime[0] = 0.8;
     attackTime[1] = 0.8;
     attackTime[2] = 0.8;
     
 }
  
 function Update() 
 {
     CheckForPlayerAttackInput();
 }
  
 function CheckForPlayerAttackInput() 
 {
     switch( attackState ) // what state is the combo in
     {
        case attack.Idle :
          if ( Input.GetMouseButtonDown(0) ) // start attacking
          {
              attackType = 0;
              attackState = attack.Attacking;
              attackTimer = attackTime[0];
              animation.CrossFade("Attack");
              // tell animation to play attack 0 (first attack)
              totalChainedHits = 1;
          }
        break;
  
        case attack.Attacking :
          attackTimer -= Time.deltaTime; // wait for attackTimer
  
          if ( attackTimer < 0 )
          {
              attackTimer = waitTime;
              attackState = attack.Waiting;
          }
  
          /* // uncomment this to break the combo while attacking
          if ( Input.GetMouseButtonDown(0) ) // break attack
          {
              attackType = -1;
              attackState = attack.Idle;
              // tell animation to play Idle
          }
          */
        break;
  
        case attack.Waiting :
          attackTimer -= Time.deltaTime; // check the Waiting Timer (attackTimer)
  
          if ( attackTimer < 0 ) // ran out of time to chain combo
          {
              attackType = -1;
              attackState = attack.Idle;
              animation.CrossFade("Idle");
              // tell animation to play Idle
          }
  
          if ( Input.GetMouseButtonDown(0) ) // continue attacking
          {
           attackType ++; // go to next attack
  
           if ( attackType >= attackTime.Length ) // check if the combo is over, start a new combo
           {
               attackType = 0;
               attackState = attack.Attacking;
               attackTimer = attackTime[0];
               animation.CrossFade("Attack");
               // tell animation to play attack 0 (first attack)
               totalChainedHits ++;
           }
           else
           {
               attackState = attack.Attacking;
               animation.CrossFade("Attack1");
               attackTimer = attackTime[ attackType ];
               // tell animation to play attack 'attackType' (next attack in array)
               totalChainedHits ++;
           }
          }
        break;
     }
 }
  
 // example GUI for testing this script
 function OnGUI() 
 {
     GUI.Box( Rect( 10, 10, 100, 25 ), "Hit " + attackType.ToString() );
     GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "" + attackState.ToString() );
     GUI.Box( Rect( Screen.width - 110, 10, 100, 25 ), "Total " + totalChainedHits.ToString() );
     GUI.Box( Rect( (Screen.width * 0.5) - 50, 45, 100, 25 ), "" + attackTimer.ToString() );
 }`
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

0 Replies

· Add your reply
  • Sort: 

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

18 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 avatar image

Related Questions

Storing button input and tags and then transfering information to dropdown menu 1 Answer

How would you store child Tags(colours) in an Array to detect if grid [x,y] && grid [x + 1, y] are the same colour? 0 Answers

How to solve the problem of loading tag changes in another project scene resource package 0 Answers

How do i destroy a particle when an object is destroyed 1 Answer

Tags do not appear on objects 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