- Home /
Cannot Jump While Moving Diagonally
I've made a simple movement script for my player (which is just a plain capsule with a rigidbody), and it works well, except I can't jump while holding W and A at the same time. All other combinations (WD, SA, SD, all arrow keys) work fine, but I cannot jump while holding W and A. Here's the movement code:
void Update() {
float dt = Time.deltaTime;
Vector3 movement =
new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (movement.magnitude > 1) //Normalizing movement
movement = movement / movement.magnitude;
transform.Translate( movement*dt*moveSpeed );
if (IsGrounded() && Input.GetButtonDown("Jump")) {
//rb is the player's rigidbody
rb.velocity = new Vector3(rb.velocity.x, jumpStrength, rb.velocity.z);
}
}
Edit: My IsGrounded() function shouldn't be affected by diagonal movement, as it just checks if a BoxCollider under the player is overlapping with the ground. Here's all the code involved:
private bool grounded = true;
//...
public bool IsGrounded() {
return grounded;
}
void OnTriggerEnter() {
grounded = true;
}
void OnTriggerStay() {
grounded = true;
}
void OnTriggerExit() {
grounded = false;
}
Setting grounded
to public shows it works correctly, and I can jump properly going any other diagonal direction (and all directions when using the arrow keys). Literally the only scenario where I can't jump is when I'm holding both the W and A keys.
is your IsGrounded() method maybe affected by moving diagonally?
Use "Debug.Log(IsGrounded());" before the last if statement, and check if it is true while using W and A. If so, post your IsGrounded() code so we can help you.
Have you tried if all the key events are triggered? $$anonymous$$aybe it's just a ghosting problem
Never do any physics in Update(). Use FixedUpdate() ins$$anonymous$$d
Never do any transform manipulations with objects that have Rigidbody attached to them unless you understand what's the difference. Use Rigidbody.position, for example. In FixedUpdate(), of course.
I bet, if you will fix this, your problem can be solved. Try ins$$anonymous$$d of doing transform.Translate use something like
rigidbody.velocity = maxVelocity * inputVector;
Answer by Kali2048 · Jan 20, 2016 at 07:30 AM
It may be your keyboard hardware problem, see this very similar problem : http://answers.unity3d.com/questions/162207/why-cant-i-firing-a-bullet-when-holding-down-left.html
It's possible, some keyboards only allow a limited amount of inputs at any one time (2-3). And I don't see anything wrong with his script.
If it was the inputs amount limitation, it will have problems with any diagonal movement + jump...
If it was the inputs amount limitation, it will have problems with any diagonal movement + jump...
True, but it's not a limitation of x amount of simultaneous key presses. Ghosting can happen on different key combinations depending on the hardware. Some combo of 4 keys might work totally fine while some combo of 3 does not.
Like I said in my earlier comment, it should be easy to detect though so we can rule this possibility out or confirm it... OP should just put some Debug.Log in Input.Get$$anonymous$$ey or Get$$anonymous$$eyDown to see if the presses trigger at all.
I opened Notepad++ and confirmed the problem is my keyboard. I'll just use a controller, I guess.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060843im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Input.GetAxis returning nonsensical values for HID Sensor/GameController 0 Answers
[SOLVED] Input.GetAxis("Horizontal") returns wrong value? -1 Answers
How to get ScrollWheel values using a laptop touchpad? 1 Answer
Get axis literal keyboard or mouse value... not possible? 0 Answers