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.

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