using System.Collections; using System.Collections.Generic; using TreeEditor; using UnityEngine; public class PlayerController : MonoBehaviour { Rigidbody2D rbody; float axisH = 0.0f; // ÀÔ·Â public float speed = 3.0f; // À̵¿ ¼Óµµ public float jump = 9.0f; public LayerMask groundLayer; bool isDead = false; bool onGround = false; Animator animator; public string stopAnime = "PlayerStop"; public string moveAnime = "PlayerMove"; public string jumpAnime = "PlayerJump"; public string goalAnime = "PlayerGoal"; public string deadAnime = "PlayerOver"; string nowAnime = ""; string oldAnime = ""; // Start is called before the first frame update void Start() { rbody = GetComponent(); //Animator °¡Á®¿À±â animator = GetComponent(); nowAnime = stopAnime; oldAnime = stopAnime; } // Update is called once per frame void Update() { axisH = Input.GetAxisRaw("Horizontal"); if(isDead) { return; } if(axisH > 0.0f) { Debug.Log("¿À¸¥ÂÊ À̵¿"); transform.localScale = new Vector2(1, 1); } else if (axisH < 0.0f) { Debug.Log("¿ÞÂÊ À̵¿"); transform.localScale = new Vector2(-1, 1); } } private void FixedUpdate() { onGround = Physics2D.Linecast(transform.position, transform.position - (transform.up * 0.1f), groundLayer); if (onGround || axisH != 0) { rbody.velocity = new Vector2(speed * axisH, rbody.velocity.y); } if (onGround) { Debug.Log("Á¡ÇÁ!"); Vector2 jumpPw = new Vector2(0, jump); rbody.AddForce(jumpPw, ForceMode2D.Impulse); } if(onGround) { if(axisH == 0) { nowAnime = stopAnime; } else { nowAnime = moveAnime; } } else { nowAnime = jumpAnime; } if(nowAnime != oldAnime) { oldAnime = nowAnime; animator.Play(nowAnime); } } public void Jump() { Debug.Log(" Á¡ÇÁ ¹öư ´­¸²! "); } private void OnCollisionEnter2D(Collision2D collision) { if (collision.contacts[0].normal.y > 0.7f) { onGround = true; } } private void OnCollisionExit2D(Collider2D collision) { onGround = false; } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Goal") { Goal(); } else if (collision.gameObject.tag == "Dead" && !isDead) { GameOver(); } } public void Goal() { animator.Play(goalAnime); } public void GameOver() { animator.Play(deadAnime); } }