Hi guys,
I'm currently making a top-down game that has the player character using physics (Rigidbody2D().AddForce)
You move the player using the WASD keys, however the character keeps moving forward. I want it to stop moving once the button is released. I'm still quite new to C# so some help would be very appreciated. Thanks again :)
Here's my code;
public class PlayerMovement2 : MonoBehaviour {
public float speed;
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKey (KeyCode.D)) {
GetComponent().AddForce (Vector2.right * speed);
}
if (Input.GetKey (KeyCode.A)) {
GetComponent().AddForce (Vector2.left * speed);
}
if (Input.GetKey (KeyCode.W)) {
GetComponent().AddForce (Vector2.up * speed);
}
if (Input.GetKey (KeyCode.S)) {
GetComponent().AddForce (Vector2.down * speed);
}
}
}
↧