• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DerickTP · Nov 17, 2014 at 05:07 PM · c#movementinput

Problem with inputs in command system

Hi all,

I have an annoying problem that has to do with Inputs. So I am making a simple Shooter game of my own, and the moving system is implemented via a command pattern. My keyboard inputs handler does seem to working well... Let me explain: the ship can move left, right, up and down, and it can fire. When the ship is not firing, I can move in all directions allright, diagonals and all. The problem arises when I am firing and try move in the top left diagonal... The ship either goes left or up depending on the key pressed first, but not diagonal. Worse, the key pressed is not even detected, so it is not a problem of generating the command. I have tried to work around the problem but I was somehow unable to solve it. The code of my Command handler function is bellow:

 public override List<Command> HandleInput()
 {
     List<Command> res = new List<Command>(4);

     //1- Retrieve the movement information
     //tried with direct access via keycodes
     bool up = Input.GetKey(KeyCode.UpArrow);
     bool down = Input.GetKey(KeyCode.DownArrow);
     bool right = Input.GetKey(KeyCode.RightArrow);
     bool left = Input.GetKey(KeyCode.LeftArrow);

     /*float vert = Input.GetAxis ("Vertical");
     float hor = Input.GetAxis("Horizontal");

     Debug.Log (vert + "\t" + hor);

     m_movement.x = hor;
     m_movement.y = vert;*/

     //Tried with using personalised commands
     /*bool up = Input.GetButton("Up");
     bool down = Input.GetButton("Down");
     bool right = Input.GetButton("Right");
     bool left = Input.GetButton("Left");*/

     Debug.Log(up+"\t"+down+"\t"+left+"\t"+right);

     if (up && m_object.rigidbody2D.position.y < y_min)
     { 
         m_movement.y = 1f; //m_movement is a command object
     }
     else if(down && m_object.rigidbody2D.position.y > y_max)
     {
         m_movement.y = -1f;
     }
     else
     {
         m_movement.y = 0f;
     }


     if (right && m_object.rigidbody2D.position.x < x_max)
     {
         m_movement.x = 1f;
     }
     else if(left && m_object.rigidbody2D.position.x > x_min)
     {
         m_movement.x = -1f;
     }
     else
     {
         m_movement.x = 0f;
     }    


     if(!(m_movement == m_old_movement))
     {
        // Create the command movement
         m_moveCommand.Velocity = m_movement;
         res.Add(m_moveCommand);
         m_old_movement = m_movement;
     }

     //2- Check if the firing button has gone down
     if(Input.GetButtonDown("Fire"))
     {
         res.Add(m_startFiringCommand);
     }

     //3- check if the fire button has gone up
     if(Input.GetButtonUp("Fire"))
     {
         res.Add(m_stopFiringCommand);
     }

     //4- Check if the pause button is hit
     if(Input.GetButtonDown("Pause"))
     {
         res.Add(m_pauseCommand);
     }


     //5- return the command list
     return res;

 }



Does anyone has any idea? Please don't hesitate to ask me for more information or for clarification. If the code is not enough, I'll send you a link to the work in progress version of the game...

EDIT UP :-)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

26 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How do I change this script to move object along spline ? 0 Answers

Two Horizontal Movement Axes Behaving Differently 0 Answers

Player moves too fast diagonally and accelerates too fast diagonally. 2 Answers

NullRefrenceException with new input system 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