Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.  
  8.     public float speed = 20f;
  9.     private Rigidbody2D rb;
  10.  
  11.     private bool faceRight = true;
  12.  
  13.     void Start() {
  14.         rb = GetComponent<Rigidbody2D>();
  15.     }
  16.  
  17.     private void Update() {
  18.         float moveX = Input.GetAxis("Horizontal");
  19.         rb.MovePosition(rb.position + Vector2.right * moveX * speed * Time.deltaTime);
  20.  
  21.         if (Input.GetKeyDown(KeyCode.Space)) {
  22.             rb.AddForce(Vector2.up * 8000);
  23.         }
  24.  
  25.         if (moveX > 0 && !faceRight) {
  26.             flip();
  27.         } else if (moveX < 0 && faceRight) {
  28.             flip();
  29.         }
  30.     }
  31.  
  32.     void flip() {
  33.         faceRight = !faceRight;
  34.         transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement