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.
Can you post the full script? Just because that little bit isn't enough to understand the problem.
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); } }
}
Is the script attached to an Active GameObject in the scene?
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()
{
}
}
Also like @Pengocat said, make sure that the script is attached to your GameObject you wish to controll.
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.