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
1
Question by awplays49 · Mar 20, 2015 at 11:26 PM · animationmovementanimatormovement script

Player can't move left or right while holding W and S at the same time - No code making it do that?

Here's my code. Never did I command it to not animate, let alone stop allowing me to move left and right completely, when holding W and S. I can, however, hold A and D and move up and down…

UPDATE It doesn't even recognize when I'm pressing A or D under said circumstances.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float Speed;
     public Animator AnimatorGet;
     private bool Up;
     private bool Left;
     private bool Down;
     private bool Right;
     private bool UpDown;
     private bool LeftRight;
 
     void FixedUpdate () {
         // Movement Controls and Animation Initiation
         if (Input.GetKey (KeyCode.W))
         {
             transform.position += Vector3.up * Speed * Time.deltaTime;
             Up = true;
         }
         else
         {
             Up = false;
         }
         if (Input.GetKey (KeyCode.A))
         { 
             transform.position += Vector3.left * Speed * Time.deltaTime;
             Left = true;
         }
         else
         {
             Left = false;
         }
         if (Input.GetKey (KeyCode.S))
         {
             Down = true;
             transform.position += Vector3.down * Speed * Time.deltaTime;
         }
         else
         {
             Down = false;
         }
         if (Input.GetKey (KeyCode.D))
         {
             Right = true;    
             transform.position += Vector3.right * Speed * Time.deltaTime;
         }
         else
         {
             Right = false;
         }
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 awplays49 · Mar 21, 2015 at 01:38 AM 0
Share

Halppppp :(

2 Replies

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

Answer by NoseKills · Mar 21, 2015 at 02:27 AM

That's an awful lot of redundant if's.

It's really hard to figure out what EXACTLY you want to do by looking at code that doesn't do that. But I tried to guess...

Here's your code simplified

     public float Speed;
     public Animator AnimatorGet;
     private bool Up;
     private bool Left;
     private bool Down;
     private bool Right;
     private bool UpDown;
     private bool LeftRight;
     
     void FixedUpdate () {
         Up     = Input.GetKey(KeyCode.W);
         Left     = Input.GetKey(KeyCode.A);
         Down     = Input.GetKey(KeyCode.S);
         Right     = Input.GetKey(KeyCode.D);
 
         bool isWalking = false;
         if (Up != Down) // if only up or down is pressed...
         {
                     // ...we know we are walking
             isWalking = true;
             if (Up) // up
             {
                 transform.position += Vector3.up * Speed * Time.deltaTime;
             }
             else if (Down) // or down
             {
                 transform.position += Vector3.down * Speed * Time.deltaTime;
             }
         }
         if (Left != Right) // same with left & right
         {
             isWalking = true;
             if (Left)
             { 
                 transform.position += Vector3.left * Speed * Time.deltaTime;
             }
             else if (Right)
             {
                 transform.position += Vector3.right * Speed * Time.deltaTime;
             }
         }
         AnimatorGet.SetBool ("Is Walking", isWalking);
 
         bool anyUp = Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D);
         bool anyDown = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D);
 
         if (anyUp || anyDown) // if any movement key was released or pressed...
         {
                     // ...change direction to current movement direction
             if (Up != Down)
             {
                 if (Up)
                 {
                     AnimatorGet.SetInteger ("Direction", 1);
                 }
                 else if (Down)
                 {
                     AnimatorGet.SetInteger ("Direction", 3);
                 }
             }
                     // no diagonal directions. 
                     // If we move Up and Left, direction is "4" because this Left/Right if is after Up/Down if                                       
             if (Left != Right)
             {
                 if (Left)
                 { 
                     AnimatorGet.SetInteger ("Direction", 4);
                 }
                 else if (Right)
                 {
                     AnimatorGet.SetInteger ("Direction", 2);
                 }
             }
         }
     }

Comment
Add comment · Show 7 · 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 awplays49 · Mar 21, 2015 at 02:31 AM 0
Share

Thank you, Ill try it right now.. While i have you, I noticed its not firing Get$$anonymous$$ey for A and D while holding W and S at the same time. Why would that be exactly?

avatar image NoseKills · Mar 21, 2015 at 02:44 AM 0
Share

I'm just using a 3D cube to test this and I had to replace the animation calls with Debug.Log("Is walking false"); etc. because I don't have an animation I could use to see what's happening.

For me the movement works ok. If i hold W & S, the cube doesn't move but if I press A or D on top of those, it moves sideways. I guess it might even be a limitation of your keyboard but the limit shouldn't be this low. Hard to say.

avatar image awplays49 · Mar 21, 2015 at 02:46 AM 0
Share

Okay i tried the script… I even took out all the animation to see if it was the problem (obviously not) and even this is not working.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : $$anonymous$$onoBehaviour {
 
     public float Speed;
     public Animator AnimatorGet;
     private bool Up;
     private bool Left;
     private bool Down;
     private bool Right;
 
     void FixedUpdate () {
         Up = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W);
         Left = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A);
         Down = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S);
         Right = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D);
     
         if (Up == true && Down == false)
         {
             transform.position += Vector3.up * Speed * Time.deltaTime;
         }
         if (Left == true && Right == false)
         {
             transform.position += Vector3.left * Speed * Time.deltaTime;
         }
         if (Down == true && Up == false)
         {
             transform.position += Vector3.down * Speed * Time.deltaTime;
         }
         if (Right == true && Left == false)
         {
             transform.position += Vector3.right * Speed * Time.deltaTime;
         }
 
         if (Up == true || Left == true || Down == true || Right == true)
         {
             if (Up != Down && Left != Right)
             {
                 AnimatorGet.SetBool ("Is Walking", true);
             }
         }
     }
 }
 


avatar image awplays49 · Mar 21, 2015 at 02:50 AM 1
Share

Yeah is it because i have an OSX keyboard? and a mac? how can i make the key memory bigger?

avatar image awplays49 · Mar 21, 2015 at 02:54 AM 1
Share

Ok thanks. Ill accept your answer thanks :)

Show more comments
avatar image
0
Wiki

Answer by fredlllll · Mar 21, 2015 at 07:42 AM

You might experience a phenomenom called "ghosting" (http://www.microsoft.com/appliedsciences/antighostingexplained.mspx ). it means some keyboards cant recognize more than 2 keys pressed. most computers will greet you with a warning sound when ghosting occurs. if i press w and s and then d i get a beep.

you might add some debug output to output the values your input gives you (some Debug.Log("w: ..... which then outputs "w:true s:true a:false d:false" so you can make sure that the key isnt registered (oh i just saw you already checked and a and d are not recognized)

also use the normal update for input handling http://stackoverflow.com/questions/19259097/is-it-really-wrong-to-use-input-getkey-on-fixedupdate

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 awplays49 · Mar 22, 2015 at 12:32 PM 0
Share

Yeah I just got a keyboard with 10-key rollover. Its a razer blackwidow chroma :)

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

23 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

Related Questions

A way to "freeze" the position of the animation? 1 Answer

I need help with player animation. 1 Answer

How to Apply Animations to Script 1 Answer

npc move when playing walk animation? 1 Answer

How do you move a character with physics when using Animator.setInteger to play the animations? 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