Hi guys. I’ve been trying to get a simple jump method working for awhile now, and for some reason it’s only half working? When I used GetKey, the jump method worked, just endlessly adding force as expected. When I switched to GetKeyDown, the jump method no longer added any force. I used Debug.Log on the Jump method and it returns a value, yet the Jump velocity is never applied? Here’s my code so far:
public class MovementScript : MonoBehaviour
{
public Rigidbody2D rb;
public int moveSpeed = 10;
public float jumpHeight = 12.0f;
void Awake()
{
{
rb = GetComponent();
}
}
private void Update()
{
if (Input.GetKey(KeyCode.Space))
{
Debug.Log(“Jumped!”);
rb.velocity = new Vector2(0, jumpHeight);
}
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-moveSpeed, 0);
}
else if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(moveSpeed, 0);
}
else
{
rb.velocity = new Vector2(0, 0);
}
}
}
Here, there’s a few things I tried. I tried wrapping the if statements into methods like Move() and Jump(), and calling those methods in Update() because I read that FixedUpdate() didn’t work very well with GetKeyDown(). I’m not sure if this is weird code, so if I’m writing this terribly please let me know. Like I mentioned at the beginning, earlier I had a GetKey() jump method that worked, and I went to switch to GetKeyDown(), and for some reason switching to GetKeyDown made the jump method no longer work. I Debug.Logged, and it still returns a value, just for some reason it stopped giving the rigidbody velocity. I’m at an utter loss, I’ve read for hours and this is my last stop. Thanks for any advice in advance guys.