Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class Movement : MonoBehaviour {
- public float mySpeed;
- private Rigidbody2D rb;
- private bool facingRight;
- private float health;
- private float maxHealth;
- Animator anim;
- private bool isGrounded;
- public Transform groundCheck;
- float groundRadius = 0.2f;
- public LayerMask stuffThatIsGround;
- public Slider healthBar;
- // Use this for initialization
- void Start () {
- anim = GetComponent<Animator> ();
- rb = GetComponent<Rigidbody2D> ();
- facingRight = true;
- isGrounded = false;
- maxHealth = 10;
- health = maxHealth;
- healthBar.value = CalculateHealth ();
- }
- // Update is called once per frame
- void FixedUpdate () {
- isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, stuffThatIsGround);
- anim.SetBool ("OnGround", isGrounded);
- float horizontalMovement = Input.GetAxis ("Horizontal");
- anim.SetFloat ("Speed", Mathf.Abs(horizontalMovement));
- rb.velocity = new Vector2 (horizontalMovement * mySpeed, rb.velocity.y);
- if (horizontalMovement > 0 && !facingRight) {
- Flip ();
- } else if (horizontalMovement < 0 && facingRight) {
- Flip ();
- }
- if (isGrounded && Input.GetKeyDown ("space")) {
- rb.AddForce (new Vector2 (0, 5), ForceMode2D.Impulse);
- }
- healthBar.value = CalculateHealth();
- }
- void Flip()
- {
- facingRight = !facingRight;
- Vector3 scale = transform.localScale;
- scale.x *= -1;
- transform.localScale = scale;
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- if (other.gameObject.tag == "Magnet") {
- //health = health - 1;
- health--;
- other.gameObject.SetActive (false);
- }
- if (health == 0) {
- SceneManager.LoadScene ("loser", LoadSceneMode.Single);
- }
- }
- float CalculateHealth()
- {
- return health / maxHealth;
- }
- /*
- void OnCollisionEnter2D(Collision2D other)
- {
- if (other.gameObject.tag == "Magnet") {
- SceneManager.LoadScene ("loser", LoadSceneMode.Single);
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement