Advertisement
MrsMcLead

2D Movement

Mar 8th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7. public class Movement : MonoBehaviour {
  8.  
  9.     public float mySpeed;
  10.     private Rigidbody2D rb;
  11.     private bool facingRight;
  12.     private float health;
  13.     private float maxHealth;
  14.  
  15.     Animator anim;
  16.  
  17.     private bool isGrounded;
  18.     public Transform groundCheck;
  19.     float groundRadius = 0.2f;
  20.     public LayerMask stuffThatIsGround;
  21.     public Slider healthBar;
  22.  
  23.  
  24.     // Use this for initialization
  25.     void Start () {
  26.         anim = GetComponent<Animator> ();
  27.         rb = GetComponent<Rigidbody2D> ();
  28.         facingRight = true;
  29.         isGrounded = false;
  30.         maxHealth = 10;
  31.         health = maxHealth;
  32.         healthBar.value = CalculateHealth ();
  33.     }
  34.    
  35.     // Update is called once per frame
  36.     void FixedUpdate () {
  37.  
  38.         isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, stuffThatIsGround);
  39.         anim.SetBool ("OnGround", isGrounded);
  40.         float horizontalMovement = Input.GetAxis ("Horizontal");
  41.         anim.SetFloat ("Speed", Mathf.Abs(horizontalMovement));
  42.         rb.velocity = new Vector2 (horizontalMovement * mySpeed, rb.velocity.y);
  43.  
  44.         if (horizontalMovement > 0 && !facingRight) {
  45.             Flip ();
  46.         } else if (horizontalMovement < 0 && facingRight) {
  47.             Flip ();
  48.         }
  49.         if (isGrounded && Input.GetKeyDown ("space")) {
  50.             rb.AddForce (new Vector2 (0, 5), ForceMode2D.Impulse);
  51.         }
  52.         healthBar.value = CalculateHealth();
  53.     }
  54.  
  55.     void Flip()
  56.     {
  57.         facingRight = !facingRight;
  58.         Vector3 scale = transform.localScale;
  59.         scale.x *= -1;
  60.         transform.localScale = scale;
  61.     }
  62.  
  63.     void OnTriggerEnter2D(Collider2D other)
  64.     {
  65.         if (other.gameObject.tag == "Magnet") {
  66.             //health = health - 1;
  67.             health--;
  68.             other.gameObject.SetActive (false);
  69.         }
  70.         if (health == 0) {
  71.             SceneManager.LoadScene ("loser", LoadSceneMode.Single);
  72.         }
  73.    
  74.     }
  75.  
  76.     float CalculateHealth()
  77.     {
  78.         return health / maxHealth;
  79.     }
  80.     /*
  81.     void OnCollisionEnter2D(Collision2D other)
  82.     {
  83.         if (other.gameObject.tag == "Magnet") {
  84.             SceneManager.LoadScene ("loser", LoadSceneMode.Single);
  85.         }
  86.     }*/
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement