SHOW:
|
|
- or go back to the newest paste.
| 1 | public class PlayerControls : MonoBehaviour {
| |
| 2 | ||
| 3 | public float speedX; | |
| 4 | public float jumpSpeedY; | |
| 5 | public float speed; | |
| 6 | ||
| 7 | bool facingRight, Jumping; | |
| 8 | ||
| 9 | Animator anim; | |
| 10 | Rigidbody2D rb; | |
| 11 | ||
| 12 | void Start () | |
| 13 | {
| |
| 14 | anim = GetComponent<Animator> (); | |
| 15 | rb = GetComponent<Rigidbody2D> (); | |
| 16 | facingRight = true; | |
| 17 | speedX = 10; | |
| 18 | jumpSpeedY = 500; | |
| 19 | } | |
| 20 | ||
| 21 | void Update () | |
| 22 | {
| |
| 23 | MovePlayer(speed); | |
| 24 | ||
| 25 | if (Input.GetKeyDown(KeyCode.A)) | |
| 26 | {
| |
| 27 | speed = -speedX; | |
| 28 | } | |
| 29 | else if (Input.GetKeyUp(KeyCode.A)) | |
| 30 | {
| |
| 31 | speed = 0; | |
| 32 | } | |
| 33 | if (Input.GetKeyDown(KeyCode.D)) | |
| 34 | {
| |
| 35 | speed = speedX; | |
| 36 | } | |
| 37 | else if (Input.GetKeyUp(KeyCode.D)) | |
| 38 | {
| |
| 39 | speed = 0; | |
| 40 | } | |
| 41 | - | } |
| 41 | + | |
| 42 | if (Input.GetKeyDown(KeyCode.Space)) | |
| 43 | {
| |
| 44 | Jumping = true; | |
| 45 | rb.AddForce(new Vector2(rb.velocity.x, jumpSpeedY)); | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | void MovePlayer(float playerSpeed) | |
| 50 | {
| |
| 51 | rb.velocity = new Vector3(speed, rb.velocity.y, 0); | |
| 52 | } | |
| 53 | } |