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 /
This question was closed Jul 21, 2017 at 08:25 PM by WatcherMagic for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by WatcherMagic · Jul 07, 2017 at 08:14 PM · keycodekeysoperatortwo

Delay when Animation used

So I've been working on character movement for an isometric game, where I want the character to have an animation for moving 0 degrees, 45 degrees and 90 degrees. 0 and 90 have been working just fine, but I've have trouble with 45 as it requires two buttons to be pressed simultaneously, and I don't know the code for adding button input together, nor can I find it. :/ Help would be appreciated!

 {
 //I've tried using +
         if (Input.GetKeyDown (KeyCode.W) + (KeyCode.D))
         {
             anim.SetInteger("State", 6);
         }
 //And I found && online,
             if (Input.GetKeyUp (KeyCode.W) && (KeyCode.D))
         {
             anim.SetInteger("State", 0);
         }
 }

but both give me errors such as "Operator '+' cannot be applied to operands 'bool' and 'UniyEngine.KeyCode'

Again, help would be great. Thanks!

EDIT: Ok, the error was fixed but the particular animation won't play. It'll play the one for 90 degrees and 0 degrees, but no 45.

EDIT AGAIN: With help (thank you) I managed to get the animation to play when both buttons are pressed down, unfortunately there was a significant time delay between pressing the buttons and the animation playing, except for the "D" key. Any solutions?

 using UnityEngine;
 using System.Collections;
 
 public class Player_Animator_Control : MonoBehaviour
 {
 
     Animator anim;
 
     // Use this for initialization
     void Start ()
     {
         anim = GetComponent<Animator> ();
     }
 
     // Update is called once per frame
     void Update ()
     {
          {        
             // Seems in the new state both D and W are pressed
             if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
             {        
                 //Move at a 45 degree angle
                 anim.SetInteger ("State", 6);
             }
             // Seems in the new state W is pressed, D is not
             else if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D))
             {        
                 //Move at a 90 degree angle
                 anim.SetInteger ("State", 7);
             }
             // Seems in the new state D is pressed, W is not
             else if (!Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
             {        
                 //Move at a 0 degree anle
                 anim.SetInteger ("State", 5);
             }
             // Neither pressed
              else
              {        
                 anim.SetInteger ("State", 0);
             }
         }
     }
 }
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

  • Sort: 
avatar image
0

Answer by FlaSh-G · Jul 07, 2017 at 09:03 PM

A method call looks like this:

 NameOfTheMethod(parameters)

And your code has that:

 Input.GetKeyDown(KeyCode.W)

which is fine. This method call returns a bool, true or false, depending on whether or not the given key is pressed at the moment. It's kinda like

 IsReady(steak)

Then, you add this behind your method call:

 + (KeyCode.D)

so you basically have

 IsReady(steak) + potato

If the steak is ready, you get

 true + potato

and oh boy, is that a complicated question to answer, right? Same with &&, which is, by the way, the correct operator to use here, being "AND". But still:

 is true and potato?

Kinda hard. So what you want to do is to ask if the steak is ready and then you ask if the potato is ready, too:

 IsReady(steak) && IsReady(potato)

which evaluates to

 true && true

and this works. True is kinda like "yes", so we have

 if yes and yes

which seems more easily to analyse. And, in fact, would be correct code. Well... semantically.

Long story short:

 if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode.D))
Comment
Add comment · Show 5 · 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 WatcherMagic · Jul 07, 2017 at 09:52 PM 0
Share

Ahh, thank you! :) That explanation actually helped a lot. The error is gone now, (it's not playing the animation, but I'll go and check my states before I ask again XP)

avatar image WatcherMagic · Jul 07, 2017 at 10:02 PM 0
Share

Nope :/ The error was fixed (thank you!) but the particular animation won't play. It'll play the one for 90 degrees and 0 degrees, but no 45.

avatar image FlaSh-G · Jul 08, 2017 at 12:02 AM 0
Share

Well... my answer was only about the scripting part. In terms of animating correctly, consider using a BlendTree rather than coding all the cases by hand.

avatar image WatcherMagic FlaSh-G · Jul 09, 2017 at 10:57 PM 0
Share

I have been using the animation tree. It works when I change the states manually in the Unity editor, just not when I play in game. Thanks for your help. :)

avatar image FlaSh-G FlaSh-G · Jul 10, 2017 at 08:30 AM 0
Share

There is no reason for it not to work ingame if you set up everything correctly, so keep trying ;)

avatar image
0

Answer by NoseKills · Jul 08, 2017 at 09:24 AM

On top of what @FlaSh-G said, there's the problem that GetKeyDown and GetKeyUp return true during that 1 frame during which the key becomes pressed or released. So...

 if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode.D))

means that those keys need to be pressed down (or released) during the same frame. If you press them at even slightly different times, the condition won't be true.

Since the animation you want to play depends on the state of the 2 buttons, you should monitor them and act if they change.

 bool wasWPressed;
 bool wasDPressed;
 
 void Update() {
     bool isWPressed = Input.GetKey(KeyCode.W);
     bool isDPressed = Input.GetKey(KeyCode.D);
 
      if (isWPressed != wasWPressed ||
           isDPressed != wasDPressed) {
         // One of the keys changed state
 
         if (isWPressed && !isDPressed) {
              // Seems in the new state W is pressed, D is not
         } else if (!isWPressed && isDPressed) {
              // Seems in the new state D is pressed, W is not
         } else if (isWPressed && isDPressed) {
              // Seems in the new state both D and W are pressed
         } else {
              // Neither pressed
         }
      }
 
     wasWPressed = isWPressed;
     wasDPressed = isDPressed;
 }


Then just trigger the right anim in the right if/else

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 WatcherMagic · Jul 08, 2017 at 04:38 PM 0
Share

Thank you, that worked! :) For a $$anonymous$$ute... Not sure what I did. I put the code in and it worked for a while, although there was a significant delay between my pressing the buttons and the animation playing. Then $$anonymous$$onoDevelop decided to pull some formatting stunt on me, and I got it back to readability but the code didn't work after that. I've checked the states; they're the right ones.

 void Update ()
     {
         bool isWPressed = Input.Get$$anonymous$$ey($$anonymous$$eyCode.W);
         bool isDPressed = Input.Get$$anonymous$$ey($$anonymous$$eyCode.D);
 
         wasWPressed = isWPressed;
         wasDPressed = isDPressed;
 
         //This makes the movement animation play when certain keys are pressed and released
         //(works by degrees; D = 0 degrees, W+D = 45 degrees, etc.
 
         // One of the keys changed state
         if (isWPressed != wasWPressed ||
             isDPressed != wasDPressed)
          {        
 
             // Seems in the new state W is pressed, D is not
             if (isWPressed && !isDPressed)
             {        
                 //$$anonymous$$ove at a 90 degree angle
                 anim.SetInteger ("State", 7);
             }
             // Seems in the new state D is pressed, W is not
             else if (!isWPressed && isDPressed)
             {        
                 //$$anonymous$$ove at a 0 degree anle
                 anim.SetInteger ("State", 5);
             }
             // Seems in the new state both D and W are pressed
              else if (isWPressed && isDPressed)
             {        
                 //$$anonymous$$ove at a 45 degree angle
                 anim.SetInteger ("State", 6);
             }
             // Neither pressed
              else
              {        
                 anim.SetInteger ("State", 0);
             }
         }
     }

Thank you for helping me ^^"

Follow this Question

Answers Answers and Comments

69 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 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 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 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

How to display the key codes of certain buttons? 0 Answers

Play animation when 2 keys pressed? 3 Answers

Integer value casting to KeyCode enum in JS? 3 Answers

keycode values for greater than and less than 1 Answer

Is there a way to combine the Update and OnGUI functions? 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