You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.6 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class player2_controls : MonoBehaviour {
  4. public Animator animator;
  5. void Start () {
  6. }
  7. public float speed;
  8. public Vector2 jumpVector;
  9. public int health = 0;
  10. private int displayedHealth = 0;
  11. private bool updateHealth = false;
  12. public GameObject bullet;
  13. public GameObject heart;
  14. bool canJump = true;
  15. public GameObject newConfetti;
  16. void Update() {
  17. Vector2 velo = rigidbody2D.velocity;
  18. velo.x = Input.GetAxis ("Horizontal_P2") * speed;
  19. rigidbody2D.velocity = velo;
  20. animator.SetFloat("Velocity",velo.magnitude);
  21. animator.SetBool ("Fall", !canJump);
  22. applyPlayerDirection (Input.GetAxisRaw ("Horizontal_P2"));
  23. if (displayedHealth != health) {
  24. updateHealth = true;
  25. }
  26. if (updateHealth)
  27. displayHealth (health);
  28. if (health == 0)
  29. death();
  30. if (Input.GetKey (KeyCode.W)){
  31. if (canJump == true){
  32. rigidbody2D.velocity = jumpVector;
  33. canJump = false;
  34. }
  35. }
  36. //if (Input.GetKey (KeyCode.Space)){
  37. //shoot
  38. //GameObject bulletGO = Instantiate (bullet) as GameObject;
  39. //bulletGO.transform.posplayteition = transform.position;
  40. //}
  41. }
  42. private void displayHealth(int health){
  43. GameObject[] hearts = GameObject.FindGameObjectsWithTag("heartUI");
  44. foreach (GameObject desHeart in hearts)
  45. GameObject.Destroy (desHeart);
  46. for (int i=1; i<= health; i++) {
  47. GameObject heartCanister = Instantiate (heart) as GameObject;
  48. Vector3 heartPos = new Vector3 ((i * 0.033f), 0.95f, 0);
  49. heartCanister.transform.position = heartPos;
  50. }
  51. displayedHealth = health;
  52. updateHealth = false;
  53. }
  54. //Changes direction of sprite according to key pressed
  55. private void applyPlayerDirection(float moveHorizontal)
  56. {
  57. if (moveHorizontal != 0)
  58. {
  59. Vector3 scale = transform.localScale;
  60. scale.x = moveHorizontal * Mathf.Abs(scale.x);
  61. transform.localScale = scale;
  62. }
  63. }
  64. private void Confettishoot(Vector3 target,float size){
  65. Debug.Log ("Creating Confetti");
  66. target.y += 1.5f;
  67. for (int i = 0; i <size; i++) {
  68. Instantiate (newConfetti, target, transform.rotation);
  69. }
  70. }
  71. private void death(){
  72. Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0);
  73. health = 3;
  74. transform.position = spawnPos;
  75. }
  76. void OnCollisionEnter2D(Collision2D col){
  77. if (col.collider.tag == "ground")
  78. canJump = true;
  79. else if (col.collider.tag == "Player") {
  80. if(transform.position.y>col.transform.position.y){
  81. col.gameObject.GetComponent<PlayerControler>().health --;
  82. Confettishoot(col.gameObject.transform.position,200.0f);
  83. //health --;
  84. rigidbody2D.velocity = jumpVector;
  85. canJump = false;
  86. }
  87. }
  88. }
  89. }