146 lines
3.1 KiB
C#
146 lines
3.1 KiB
C#
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<Rigidbody2D>();
|
|
//Animator 가져오기
|
|
animator = GetComponent<Animator>();
|
|
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);
|
|
}
|
|
}
|