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 Decreationist · Nov 21, 2016 at 07:21 AM · scripting problemattackbasicattacking

Different attacks using same keybindings?

I'm designing a rather simple combat system driven by animations.

I'm trying to create a couple of attacks with the following keybinds:

Mouse 1 = Simple Attack Mouse 2 = Different Simple Attack LeftCtrl + Mouse1 = Power Attack.

However, Unity seems to register only the mouseclick when I'm holding LeftCtrl. I'm almost certain this is just down to me being a noob at coding.

 using UnityEngine;
 using System.Collections;
 
 public class Attack : MonoBehaviour {
     Animator anim;
 
     void Awake()
     {
         anim = GetComponent<Animator>();
     }
 
     void Update()
     {
         if (anim.GetBool("CombatStance") == true)
         {
             if (Input.GetKeyDown(KeyCode.LeftControl) && Input.GetButtonDown("Fire1"))
             {
                 anim.SetTrigger("LeftPowerAttack");
             }
             else if (Input.GetButtonDown("Fire1"))
             {
                 anim.SetTrigger("LeftAttack");
             }
             else if (Input.GetButtonDown("Fire2"))
             {
                 anim.SetTrigger("RightAttack");
             }
 
         }
     }
 }
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
Best Answer

Answer by Dibbie · Nov 21, 2016 at 08:15 AM

GetKeyDown, registers ONCE when the user actually presses the key stroke, doesnt have to lift their finger off the key, just as soon as the computer registers "Left Control was pressed", it considers it down, it doesnt check "Left Control is still down" -- thats what GetKey does, itll continue to send "Left Control is held down" rather then "Left Control is pressed".

So you could try structuring your if-statements to look like:

 if (Input.GetKey(KeyCode.LeftControl) && Input.GetButtonDown("Fire1"))
              {
                  anim.SetTrigger("LeftPowerAttack");
              }
              else if (Input.GetButtonDown("Fire1"))
              {
                  anim.SetTrigger("LeftAttack");
              }
              else if (Input.GetButtonDown("Fire2"))
              {
                  anim.SetTrigger("RightAttack");
              }

Hope that helps.

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 Decreationist · Nov 21, 2016 at 04:16 PM 0
Share

This worked perfectly, thanks a ton.

avatar image
0

Answer by Steveo22 · Nov 21, 2016 at 08:18 AM

Hello Decreationist,

Based on that code, the LeftPowerAttack should be happening. I haven't messed around with checking two inputs within a single if statement, but as I said, that should be working. Two options for improvement immediately come to mind: 1. You could nest the check for leftcontrol, so like

          if (Input.GetButtonDown("Fire1"))
          {
              if(Input.GetKeyDown(KeyCode.LeftControl)){
                          anim.SetTrigger("LeftPowerAttack");
               }
               else{
                          anim.SetTrigger("LeftAttack");
          }

should work.

  1. You could have Left control be stored in a different boolean variable and have it check that instead of the actual GetKeyDown.

I also just noticed that you're using GetButtonDown for fire1, which would work if you have it set up as a button. As far as I am aware, though, "fire1" is defaultly an Axis, which means that you would measure it as a float with a range from -1 to 1. You would check this by typing if(Input.GetAxis("fire1" > 0f))

You can go into Edit > Project Settings > Input to change whether or not it is an Axis

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 Steveo22 · Nov 21, 2016 at 08:19 AM 0
Share

And I also just realized that you're using Get$$anonymous$$eyDown for leftControl. This should be Get$$anonymous$$ey, since Get$$anonymous$$eyDown only checks if the key has been pressed THIS FRA$$anonymous$$$$anonymous$$ Let me know how it goes.

avatar image Decreationist · Nov 21, 2016 at 04:17 PM 0
Share

Get$$anonymous$$eyDown was the issue, and changing it to Get$$anonymous$$ey solved the issue. Thanks.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

BEGINNER: why is my main menu not loading level 1 when I press play? 1 Answer

main menu not loading level 1 please help beginner here 0 Answers

Newbie question 2 Answers

Script problem : beat'em up 0 Answers

Timed Attack System 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