- Home /
my 2d character won't move when i hold any movement key?
i started recently learning the unity basics and programmin (about 4 weeks ago) and i'm enjoying it very much, usually whenver i'm stuck i search on the internet and i find a solution everytime but this time i didn't find anyone encountered the same problem as me so here my problem . i made a movement script to my 2d character using c# , it worked fine but to make my character move i have to click the key everytime and if i keep holding it he won't move .
public class NinjaMovement : MonoBehaviour
{
public float speed = 3f;
public Rigidbody2D rb;
public float jump = 2f;
void awake ()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
// ...moving left
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Debug.Log(rb);
rb.velocity = new Vector2(speed * -1,rb.velocity.y);
}
//...moving right
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Debug.Log(rb);
rb.velocity = new Vector2(speed , rb.velocity.y);
}
//...jumping
if (Input.GetKeyDown(KeyCode.UpArrow))
{
rb.velocity = new Vector2(jump, rb.velocity.x);
}
}
}
Answer by mustafacomert00 · Aug 28, 2021 at 01:29 PM
You should look at Input.GetKey
If you don't understand feel free to ask me on comment section.
as i saw everything in my code looks fine, i tried to do a little change from (KeyCode.LeftArrow) to ("left") and i still get the same problem
I think means to say just to use getkey and not getkeydown, as the former is a continuous inputs while the latter is not
nice it worked, thank you very much for your help.
Also non continuous inputs like getkeydown should not be used in fixedupdate
Input.GetKeyDown and Input.GetKey is different than each other, Input.GetKeyDown will be true when user hits the key, Input.GetKey will be true while user holds the key down.
If you want to move your character while holding the key use Input.GetKey or Input.GetAxis not Input.GetKeyDown.
i already solved the problem but i'll do more research on the topic, thanks anyways
Your answer
Follow this Question
Related Questions
How to make my character stop stuttering when walking down a slope? 2 Answers
Non-symmetrical Character walk left animation. 0 Answers
Rigidbody Platform Character Movement 0 Answers
Smooth Character Movement 1 Answer
Another Double Jump Question 0 Answers