#W key not working

1 messages · Page 1 of 1 (latest)

floral pelican
#

The W key on my keyboard works, but after I converted my game from my laptop to my pc, I only jump on random occasions, I tested it with a debug and I’m just randomly pressing not pressing w even when spamming it. I tested it in other projects each time it does not work. It just very rarely executes with no pattern.

floral pelican
#

If I Wright literally Debug.Log(Input.GetKeyDown(“w”)) in a void update loop with nothing else, it will randomly return true but when I’m still pressing w sometimes returns false

#

If I swap it for a different key it works

#

If I swap out the keyboard nothing changes too

surreal elk
#

Input.GetKeyDown will only return true for one frame when you start pressing the key. If you want the value to stay true as long as the key is being held, use Input.GetKey.
Also for the sake of performance, GetKeyDown should be used with the KeyCode enum as a parameter, rather than strings.

floral pelican
# surreal elk `Input.GetKeyDown` will only return `true` for one frame when you start pressing...

The jump script is suppose to do that and is suppose to return it for 1 frame. It was working before I got it on the pc. I have made some progress though, I found out it works if I use an if statement instead. I am sure it doesn’t work with debug because I did a test where I changed my max FPS to 1 so I could check my frames and it never showed true unless apart from those few frames, but now I understand it may well be a separate issue as I am using an if statement in the code.

#

Every time I randomly jump it is executed from line 344, is grounded is permanently true so it is not that, the coroutine cyotetime has been tested and is not changing IsJump to false, there is no reason for it to not jump occasionally so I’m just very confused

#

These are the jump coroutines

surreal elk
#

do not send photos of your screen. and neither images of code. please.

eager canyonBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

floral pelican
#

Oh wait I think I know the problem

#

Does adding a ! Before a condition infer that the answer would be opposite

#

Because that’s what I thought but I’m not sure

surreal elk
#

yes, an exclamation mark in front of a boolean inverts it.

floral pelican
#

Ok that’s wierd

#

So if I say if (IsGrounded) {Debug.Log(“grounded”) it displays in air but if I put an exclamation mark before it it still displays in just air

#

And yes my isgrounded works perfectly

surreal elk
#

You do realize that you are starting a new CoyoteTime coroutine every frame as long as IsGrounded is true, and each of those runs independently and potentially overrides the current state of IsGrounded?
This may or may not become an issue.

floral pelican
#

Ok idk now the debugs are working perfectly now and I changed nothing

#

Could this be one of those very rare times it’s not user error?

surreal elk
#

If so, I'd doubt it.

floral pelican
surreal elk
#

Did you follow a tutorial for the coyote time, or did you implement it on your own?

floral pelican
#

But for some reason I’m the coyote time it returns true on the if statement

#

Which is odd because if I debug weather grounded is true before, it will say yes

#

Which is why I was considering that the issue is because i did !IsGrounded

surreal elk
#

Frankly, I would just do something like this for Coyote Time: ```cs
private float coyoteTime;
private bool groundState;
[SerializeField] private float coyoteDuration = 0.5f;

public bool IsGrounded { get; private set; }

private void Update ()
{
UpdateGroundState();
UpdateIsGrounded();
}

private void UpdateGroundState()
{
// do some kind of ground check and set "groundState".
}

private void UpdateIsGrounded ()
{
if (groundState) // if we are actually grounded, reset coyote time
{
coyoteTime = coyoteDuration;
}
else // if we are not actually grounded
{
coyoteTime -= Time.deltaTime; // decrease coyote time
}

IsGrounded = groundState || coyoteTime > 0f; // set IsGrounded to true as long as we are actually grounded, or coyoteTime is greater than 0.
}```no coroutines needed.

floral pelican
#

What would the explanation for it working on my laptop but not my@pc be tho?

#

Because i feel like it’s not a coroutine problem for that reason

surreal elk
#

Perhaps there it runs on a different framerate, and that changes the behaviour 🤷‍♂️

floral pelican
#

Maybe

#

Good idea actually