Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

How to stop the character moving after the button is pressed, using Rigidbody2D()..AddForce?

$
0
0
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); } } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>