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
1
Question by Kilimine · Dec 18, 2016 at 11:13 PM · inputdebugkeyboardloggetkey

Keyboards Input not working

I'm making a small 2D platformer game to learn the basics of programming in c# and I quickly encountered a problem. After writing the lines of code necessary for my character to move, I tested and nothing happenned. I know most would think it is an error with the script and I do not dismiss that but, there seems to be another problem going on.

I wrote a small piece of script to detect input from a key and notify in the concole that it is working but, it's not working. I have tested the script with different keys and also with the horizontal axis but nothing works.

Here is the script to notify me of the input working properly:

     if(Input.GetKey(KeyCode.A))
     {
         Debug.Log("Working");
     }

This script is written in the update "section".( void update(){ here } )

p.s. There might be a very simple solution to this matter but I do not know of it.

Edit: Here is the complete script of the controls for my character + the test debug script: (please note the values are values placed there temporarely and the values might be unsuited for an actual game but the problem that I have is stopping me from testing those values.)

-–----

private Rigidbody2D myRigidBody;

private float moveSpeed = 2f;

private bool grounded;

public float jumpHeight = 0f;

public Transform groundcheck;

void Start () {

  myRigidBody = GetComponent<Rigidbody2D>();

}

// Update is called once per frame

void Update () {

  grounded = Physics2D.Linecast(transform.position, groundcheck.position, 1 << LayerMask.NameToLayer("Ground"));
  if (Input.GetKeyDown(KeyCode.B) && grounded)
  {
      myRigidBody.velocity = new Vector2(0,jumpHeight); 
  }
  if (Input.GetKey(KeyCode.Q))
  {
      Debug.Log("Working");
  }
  if (Input.GetKey(KeyCode.A)){
      myRigidBody.velocity = new Vector2(moveSpeed * -1, 0);
  }
  if (Input.GetKey(KeyCode.D)){
      myRigidBody.velocity = new Vector2(moveSpeed * 1, 0);
  }

}

} Edit2: I tried using the mouse buttons to see if anything works but it didn't work... seems like no inputs affecting the game are getting received.

Edit3: It suddenly started working for no reason so I guess now everything's fine.

Comment
Add comment · Show 4
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 SoraMahiro · Dec 18, 2016 at 11:16 PM 0
Share

Can you post the full script? Just because that little bit isn't enough to understand the problem.

avatar image Kilimine SoraMahiro · Dec 19, 2016 at 12:39 AM 0
Share

Here is the complete script of the controls for my character + the test debug script: (please note the values are values placed there temporarely and the values might be unsuited for an actual game but the problem that I have is stopping me from testing those values. Also updated the original post since code can be presented more clearly on that.)

private Rigidbody2D myRigidBody; private float moveSpeed = 2f; private bool grounded; public float jumpHeight = 0f; public Transform groundcheck; void Start () { myRigidBody = GetComponent(); }

// Update is called once per frame void Update () { grounded = Physics2D.Linecast(transform.position, groundcheck.position, 1 << Layer$$anonymous$$ask.NameToLayer("Ground")); if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.B) && grounded) { myRigidBody.velocity = new Vector2(0,jumpHeight); } if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q)) { Debug.Log("Working"); } if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){ myRigidBody.velocity = new Vector2(moveSpeed -1, 0); } if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){ myRigidBody.velocity = new Vector2(moveSpeed 1, 0); } }

}

avatar image Pengocat · Dec 19, 2016 at 05:29 PM 0
Share

Is the script attached to an Active GameObject in the scene?

avatar image Kilimine Pengocat · Dec 22, 2016 at 01:24 AM 0
Share

Yes it is, that's not the problem

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SoraMahiro · Dec 19, 2016 at 06:16 PM

Here, try this. If it doesn't work I don't know what to say:

 public class Sample:MonoBehaviour
 {
     Rigidbody2D rig;
     const float jumpHeight = .5f;
     float moveSpeed;
 
     public void Start()
     {
         rig = GetComponent<Rigidbody2D>();
         moveSpeed = 2f;
     }
     public bool isGrounded(Vector2 sprite, Vector2 ground)
     {
         if (Physics2D.Linecast(sprite, ground, 1 << LayerMask.NameToLayer("Ground")))
         {
             return true;
         }
         return false;
     }
     public void checkInput()
     {
         Vector2 ground = new Vector2(0, 0); //Psuedo, can be any vector position that you want, perhaps current postition so you could jump any time
         if (Input.GetKeyDown(KeyCode.B) && isGrounded(transform.position, ground))
         {
             rig.velocity = new Vector2(0, jumpHeight);
         }
         else if (Input.GetKeyUp(KeyCode.Q))
         {
             Debug.Log("Working");
         }
         else if (Input.GetKeyUp(KeyCode.A))
         {
             rig.velocity = new Vector2(moveSpeed * -1, 0);
         }
         else if (Input.GetKeyUp(KeyCode.D))
         {
             rig.velocity = new Vector2(moveSpeed * 1, 0);
         }
     }
     public void Update()
     {
         
     }
 }
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 SoraMahiro · Dec 19, 2016 at 06:29 PM 0
Share

Also like @Pengocat said, make sure that the script is attached to your GameObject you wish to controll.

avatar image Kilimine · Dec 22, 2016 at 01:44 AM 0
Share

I copy pasted the script and, it didn't work. I made sure the script was activated on the character (who is active) but still, nothing happened. It's weird because it seems like only the keyboads input that affect the game or the log don't work as the keys Q, W, E, R and T can be used perfectly fine to navigate amongst the tweaking options for the Scene tab. Yeah, it's weird and annoying since it stops me from testing the values of the movespeed and jump height. It also stops my progress as I'm not sure everything is right with my script. But yeah, it didn't work, unfortunately.

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

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

Why unity not working after launching .exe but works when build and run? 0 Answers

Input Keyboard GetAxis not working 0 Answers

Rebind - Excluding Certain Keys 0 Answers

Script wont work when Debug log removed 0 Answers

Custom keyboard layout for mobile game. 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