-
04/13 유니티 쿠키런 제작 2유니티 2021. 4. 13. 17:44
체력바 추가
죽는 애니메이션 추가
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class PlayerController : MonoBehaviour { public float jumpForce = 700f; private int jumpCount = 0; private bool isGrounded = false; private bool isDoubleJump = false; private bool isSlide = false; private Rigidbody2D playerRigidbody; private Animator animator; private BoxCollider2D[] colliders; public GameObject[] scrollingObject; public Button btnJump; public Button btnSlide; // Start is called before the first frame update void Start() { this.playerRigidbody = this.GetComponent<Rigidbody2D>(); this.animator = this.GetComponent<Animator>(); this.colliders = this.GetComponents<BoxCollider2D>(); this.scrollingObject = GameObject.FindGameObjectsWithTag("Map"); this.btnJump.onClick.AddListener(() => { if(this.animator.GetFloat("Hp") == 0) { return; } if (this.jumpCount < 2) { this.jumpCount++; playerRigidbody.velocity = Vector2.zero; this.playerRigidbody.AddForce(new Vector2(0, this.jumpForce)); if (this.jumpCount > 1) { this.isDoubleJump = true; } } else if (this.playerRigidbody.velocity.y > 0) { this.playerRigidbody.velocity = this.playerRigidbody.velocity * 0.5f; } }); this.btnSlide.onClick.AddListener(() => { }); } // Update is called once per frame void Update() { this.animator.SetBool("DoubleJump", this.isDoubleJump); this.animator.SetBool("Grounded", this.isGrounded); this.animator.SetBool("Slide", this.isSlide); if(this.animator.GetFloat("Hp") == 0) { for(int i = 0; i < scrollingObject.Length; i++) { scrollingObject[i].GetComponent<ScrollingObject>().speed = 0; } } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.contacts[0].normal.y > 0.7f) { this.isGrounded = true; this.isDoubleJump = false; this.jumpCount = 0; } } private void OnCollisionExit2D(Collision2D collision) { if(this.transform.position.y > 0.1f) { isGrounded = false; } } public void SlideButtonDown() { if (this.animator.GetFloat("Hp") == 0) { return; } colliders[0].enabled = false; colliders[1].enabled = true; isSlide = true; } public void SlideButtonUp() { if (this.animator.GetFloat("Hp") == 0) { return; } colliders[0].enabled = true; colliders[1].enabled = false; isSlide = false; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HpGauge : MonoBehaviour { public Image img; public GameObject eff; public Animator animator; private float width; // Start is called before the first frame update void Start() { this.width = this.img.GetComponent<RectTransform>().rect.width; } // Update is called once per frame void Update() { var pos = this.eff.transform.localPosition; pos.x = this.img.fillAmount * this.width - 12f - 2f; this.eff.transform.localPosition = pos; this.img.fillAmount -= 0.05f * Time.deltaTime; if(this.animator.GetFloat("Hp") != 0) { this.animator.SetFloat("Hp", img.fillAmount * 100); } } }
'유니티' 카테고리의 다른 글
04/15 유니티 IK 연습 (0) 2021.04.15 04/14 수업내용 메모 (0) 2021.04.14 04/13 유니티 쿠키런 제작 1 (1) 2021.04.13 04/11 유니티 조이스틱으로 캐릭터 이동 연습 (0) 2021.04.12 04/09 수업내용 메모 (0) 2021.04.09