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 /
  • Help Room /
avatar image
0
Question by Gimly161 · Jul 29, 2016 at 02:27 PM · inputanimationsif statementsound on key

if statement not working correctly with keyinput

okey sorry for my bit messy code but here it is

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Movement1 : MonoBehaviour
 {
 
     public float speedF = 5;
     public float speedFF = 1.8F;
     public float speedB = 1.8F;
     public float TurnSpeed = 200;
     public float JumpSpeed = 10;
     public float i;
     public bool runF;
     public bool runB;
     public bool fall;
     public bool MayJump;
     public bool startedCounting;
     public bool teleported;
     public bool Jump;
     public bool death;
     public Transform Player;
     public Rigidbody PlayerRagdoll;
     public Animator anim;
     public int CollisionCount = 0;
     public Slider slider;
     public AudioSource walksound;
     private Rigidbody rb;
     
 
     // Use this for initialization
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         startedCounting = false;
         death = false;
         slider = Slider.FindObjectOfType<Slider>();
         AudioSource walksound = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
         {
             walksound.Stop();
         }
         if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) && !walksound.isPlaying)
         {
             walksound.Play();
             walksound.loop = true;
         }
         
         //if moving set run true
         if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) && runB != true)
         {
             runF = true;
         }
         else
         {
             runF = false;
         }
         if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) && runF != true)
         {
             runB = true;
         }
         else
         {
             runB = false;
         }
 
 
         // if running play run animation
         if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) && runB != true && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("run") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
         {
             anim.Play("run", 0, 0f);
             
         }
         if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) && runF != true && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("back") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
         {
             anim.Play("back", 0, 0f);
         }
         if (Input.GetKey(KeyCode.Space) && MayJump == true)
         {
             Jump = true;
         }
         else
         {
             if(CollisionCount != 0)
             {
                 Jump = false;
             }           
         }
         slider.value -= Time.deltaTime;
         // if not running play idle animation
         if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) && runB != true && runF != true)
         {
             anim.Play("idle", 0, 0f);
         }
         if (fall == false)
         {
             anim.Play("idle", 0, 0f);
             fall = true;
         }
 
         if(CollisionCount == 0)
         {
             StartCoroutine("WaitSeconds");
         }
         else
         {
             startedCounting = false;
         }
         //if no collsion play falling animation
         if (CollisionCount == 0 && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall") && startedCounting == true)
         {
             anim.Play("fall", 0, 0f);
         }
         if(CollisionCount > 0)
         {
             if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
             {
                 fall = false;
             }
         }    
         if(GameObject.Find("Stick_Figure_T-pose ragdoll(Clone)"))
         {
             Destroy(gameObject);
         }
         if(slider.value == 0)
         {
             MayJump = false;
         }
         else
         {
             MayJump = true;
         }
     }
 

please try to ignore everything that doesn't have to do with animations. so my problem is that if I run with w a s d i run perfectly fine but when i try and run with the arrow keys my animation starts every frame as if

 !this.anim.GetCurrentAnimatorStateInfo(0).IsName("run")

doesn't exist.

but i also put some sound in the walking called walking sound but if i walk with w a s d the sound starts every frame as if

!walksound.isPlaying

doesn't exist but when i run with the arrow keys the sound works perfectly and stops like it should. Now the strange thing is that I used || in every if statement that asks input like

if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) {

}

so I have no clue why my code doesn't work properly.

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 Gimly161 · Jul 29, 2016 at 02:31 PM 0
Share

just when i posted this i saw a mistake in the sound code so the sound is working now but my animations played while moving with arrow keys still don't work

1 Reply

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

Answer by TBruce · Jul 29, 2016 at 06:14 PM

The first two if statements in your Update() statement are incorrect.

Try the modified Update() function below.

 void Update()
 {
     bool upKeyReleased = false;
     bool downKeyReleased = false;
     bool leftKeyReleased = false;  // not currently used
     bool rightKeyReleased = false; // not currently used
     bool upKeyPressed = false;
     bool downKeyPressed = false;
     bool leftKeyPressed = false;   // not currently used
     bool rightKeyPressed = false;  // not currently used
     bool jumpKeyPressed = false;
 
 
     if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
     {
         upKeyReleased = true;
     }
     else if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))
     {
         downKeyReleased = true;
     }
     else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))
     {
         leftKeyReleased = true;
     }
     else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
     {
         rightKeyReleased = true;
     }
     else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
     {
         upKeyPressed = true;
     }
     else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
     {
         downKeyPressed = true;
     }
     else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
     {
         leftKeyPressed = true;
     }
     else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
     {
         rightKeyPressed = true;
     }
 
     if (upKeyReleased)
     {
         walksound.Stop();
     }
     else if (upKeyPressed && (!walksound.isPlaying))
     {
         walksound.Play();
         walksound.loop = true;
     }
 
     //if moving set run true
     if (upKeyPressed && (!runB))
     {
         runF = true;
     }
     else
     {
         runF = false;
     }
 
     if (downKeyPressed && (!runF))
     {
         runB = true;
     }
     else
     {
         runB = false;
     }
 
 
     // if not running running foreward animation and not falling
     if (upKeyPressed && !runB && !fall && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("run") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
     {
         anim.Play("run", 0, 0f);
     }
 
     // if not running running backward animation and not falling
     if (upKeyPressed && !runF && !fall && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("back") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
     {
         anim.Play("back", 0, 0f);
     }
 
     if (jumpKeyPressed && MayJump == true)
     {
         Jump = true;
     }
     else
     {
         if(CollisionCount != 0)
         {
             Jump = false;
         }           
     }
 
     slider.value -= Time.deltaTime;
     // if not running play idle animation
     if ((upKeyReleased || downKeyReleased) && (!runB) && (!runF))
     {
         anim.Play("idle", 0, 0f);
     }
 
     if (!fall)
     {
         anim.Play("idle", 0, 0f);
         fall = true;
     }
 
     if(CollisionCount == 0)
     {
         StartCoroutine("WaitSeconds");
     }
     else
     {
         startedCounting = false;
     }
 
     //if no collsion play falling animation
     if (CollisionCount == 0 && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall") && startedCounting == true)
     {
         anim.Play("fall", 0, 0f);
     }
 
     if(CollisionCount > 0)
     {
         if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
         {
             fall = false;
         }
     }    
 
     if(GameObject.Find("Stick_Figure_T-pose ragdoll(Clone)"))
     {
         Destroy(gameObject);
     }
 
     if(slider.value == 0)
     {
         MayJump = false;
     }
     else
     {
         MayJump = true;
     }
 }
Comment
Add comment · Show 3 · 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 Gimly161 · Jul 29, 2016 at 07:35 PM 0
Share

@$$anonymous$$avina well thank you very much it works now however I made my response so late because I had to fix a few things especially the animations en the jumping however I troubles hooted it fairly quick for a beginner (I'm program$$anonymous$$g for almost 2 months now so I see myself as a beginner). this is the end result

Code moved here for length reasons.

I moved all those booleans to the top and you forgot to make it possible to make those booleans false and the jumping variables got messed up but everything works perfectly now thank you very much!

avatar image TBruce Gimly161 · Jul 29, 2016 at 08:32 PM 0
Share

Unless for some reason you need to use the outside the Update() function, these need to either be be local to the Update() function.

 public bool up$$anonymous$$eyReleased = false;
 public bool down$$anonymous$$eyReleased = false;
 public bool left$$anonymous$$eyReleased = false;  // not currently used
 public bool right$$anonymous$$eyReleased = false; // not currently used
 public bool up$$anonymous$$eyPressed = false;
 public bool down$$anonymous$$eyPressed = false;
 public bool left$$anonymous$$eyPressed = false;   // not currently used
 public bool right$$anonymous$$eyPressed = false;  // not currently used
 public bool jump$$anonymous$$eyPressed = false;

If you keep the variables above the way they are you still need to make sure that you reset them all to false at the top of the Update() function. Becaus if you donot the next time the Update function is entered previous values can be retained.

You can also place everything in a separate function and call it when first entering Update(), Like so

 void Get$$anonymous$$eyInput()
 {
     // reset all flags
     up$$anonymous$$eyReleased = false;
     down$$anonymous$$eyReleased = false;
     left$$anonymous$$eyReleased = false;  // not currently used
     right$$anonymous$$eyReleased = false; // not currently used
     up$$anonymous$$eyPressed = false;
     down$$anonymous$$eyPressed = false;
     left$$anonymous$$eyPressed = false;   // not currently used
     right$$anonymous$$eyPressed = false;  // not currently used
     jump$$anonymous$$eyPressed = false;
 
     if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.UpArrow))
     {
         up$$anonymous$$eyReleased = true;
     }
     else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.S) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.DownArrow))
     {
         down$$anonymous$$eyReleased = true;
     }
     else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow))
     {
         left$$anonymous$$eyReleased = true;
     }
     else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.RightArrow))
     {
         right$$anonymous$$eyReleased = true;
     }
     else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
     {
         up$$anonymous$$eyPressed = true;
     }
     else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
     {
         down$$anonymous$$eyPressed = true;
     }
     else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
     {
         left$$anonymous$$eyPressed = true;
     }
     else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
     {
         right$$anonymous$$eyPressed = true;
     }
     else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
     {
         jump$$anonymous$$eyPressed = true;
     }
 }

and inside Update just call

 Get$$anonymous$$eyInput();
avatar image Gimly161 · Jul 29, 2016 at 07:36 PM 0
Share

to be sure you'll see my "answer" I comment also on this post

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

68 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

Related Questions

Warning: Noob question imminent... 0 Answers

Oculus UI click firing previously clicked UI button before current clicked UI button 1 Answer

UNITY no longer detecting XBOX360 controller input,XBOX 360 controller no longer working with UNITY 0 Answers

Can't disable Mouse wheel's navigation actions,Can't disable Mouse wheel's navigation 0 Answers

Using Nintendo Switch pro controller 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