This is a neat issue, I don’t really understand. It only happens whenever I’m walking, but on occasion, my Y velocity will be double the intended amount whenever I press the jump button.
using UnityEngine;
public class InGameControls : MonoBehaviour {
float keyboardMoveSpeed = 150.0f;
public float jumpPower = 5.0f;
public Rigidbody2D playerRigidbody;
//There's a regular box collider 2D used by the player
//and an additional one at the feet that's a trigger, not overlapping.
void OnTriggerStay2D() {
if (Input.GetKey(KeyCode.Space)) {
playerRigidbody.AddForce (transform.up * jumpPower, ForceMode2D.Impulse);
}
if (!Input.anyKey && playerRigidbody.velocity.y == 0) {
Vector2 temp = new Vector2 (0, playerRigidbody.velocity.y);
playerRigidbody.velocity = temp;
}
}
void Update() {
if (Input.GetKey(KeyCode.A)) {
playerRigidbody.velocity = new Vector2(-keyboardMoveSpeed * Time.deltaTime, playerRigidbody.velocity.y);
}
else if (Input.GetKey(KeyCode.D)) {
playerRigidbody.velocity = new Vector2(keyboardMoveSpeed * Time.deltaTime, playerRigidbody.velocity.y);
}
}
}
Edit: Updated code to reflect current state.