- Home /
Velocity based movement (2D)
So I came across an issue with my movement for my character in this game. The left control works perfectly but when use the right arrow key nothing happens. When i remove the code for the left key the right key works. When i place the left key code above the right key code neither of them work. Here is the code.` public float speed = 8f; public float upSpeed = 5f; public Rigidbody2D player;
// Use this for initialization
void Start () {
player = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.RightArrow)) {
player.velocity = new Vector2 (speed, 0);
}
else {
Input.GetKeyUp(KeyCode.RightArrow);
player.velocity = new Vector2(0, 0);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
player.velocity = new Vector2 (-speed , 0);
}
else {
Input.GetKeyUp(KeyCode.LeftArrow);
player.velocity = new Vector2(0, 0);
}
float upMove = Time.deltaTime * 5;
transform.Translate(0, upMove, 0, Space.World);
}
Answer by Eno-Khaon · Oct 18, 2015 at 10:06 PM
Okay, this is a little bit of a mess right now, so first off, here's what's happening with your code at the moment:
First, you check (per frame) whether the right arrow key is actively held. If it is, you move right.
However, if you're not holding right, your formatting is wrong. You're using
else {
Input.GetKeyUp(KeyCode.RightArrow);
}
when, logically, you would want something more like
else if(Input.GetKeyUp(KeyCode.RightArrow)) {
// ...
}
The same problem applies on the second round with the left arrow key.
However, there's an even simpler approach to all of this in the first place. If you make use of Unity's control systems, you can use GetAxis() (or GetAxisRaw()) to define your control scheme in fewer terms with reasonable accuracy.
To give an example of this:
void Update () {
float inputHorizontal = Input.GetAxisRaw("Horizontal");
player.velocity = Vector2.right * inputHorizontal * speed;
// ...
}
In this manner, your inputs are controlled by left and right (or, for that matter, A and D in the WASD control scheme) and are theoretically customizable. Additionally, it automatically balances out the input back to 0 if you hold both to the left and right simultaneously.
Edit: That said, I would generally recommend against directly setting the velocity vector directly, and would instead suggest utilizing AddForce(), as per the typical recommendations. Doing so involves a fair bit more work, however, but can result in very clean gameplay.
Thanks a lot. I appreciate this, I'm still really new to Unity and C#(coding in general too), I used the Input.GetAxisRaw("Horizontal") but the positive and negative buttons seem to be inverted (Right moves it left, Left moves it right). I checked my input manager to see if everything was right and it was. Right was set as positive and left set as negative.
EDIT- Ins$$anonymous$$d of Vector2.right i used Vector2.Left and it seems to be working exactly how I want it!
Your answer
Follow this Question
Related Questions
Return Proper Velocity of Player object 3 Answers
Rigidbody player movement unrealistic velocity problem. 1 Answer
Stopping an object immediately 1 Answer
Slowing Down When Sliding Across Walls 2 Answers
Player Going through colliders 1 Answer