먼저 벽과 공(캐릭터)간의 충돌을 감지할 수 있도록 했다.

 

    private void OnCollisionEnter2D(Collision2D other) 
    {
        if (other.gameObject.tag == "Wall")
        {
            Debug.Log("GameOver");
        }
    }

 

간단하게 콘솔창을 통해 충돌을 감지하는지 확인했다.

 


그 후에는 player가 pointbox에서 키를 눌려 점수를 올릴 수 있도록 했다.

 

Player c#스크립트

public class PlayerMove : MonoBehaviour
{   
    public bool isPoint = false;
    public int point = 0;
    public float Speed;
    private int directionIs = 0;
    Rigidbody2D rigid;

    // Start is called before the first frame update
    void Awake()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Rotation();
        Rotate();
    }

    void FixedUpdate() 
    {
        Move();
    }
    private void Rotation()
    {
        //키입력 시에 방향전환
        if (Input.GetKeyDown(KeyCode.D))
        {
            if (directionIs == 0)
                directionIs = 3;
            else
                directionIs = 0;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (directionIs == 1)
                directionIs = 2;
            else
                directionIs = 1;
        }
        
    }

    private void Move()
    {
        if (directionIs==0)
            transform.position = new Vector2(transform.position.x, transform.position.y + Speed);
        else if (directionIs==1)
            transform.position = new Vector2(transform.position.x + Speed, transform.position.y);
        else if (directionIs==2)
            transform.position = new Vector2(transform.position.x - Speed, transform.position.y);
        else if (directionIs==3)
            transform.position = new Vector2(transform.position.x, transform.position.y - Speed); 
    }

    private void OnCollisionEnter2D(Collision2D other) 
    {
        if (other.gameObject.tag == "Wall")
        {
            Debug.Log("GameOver");
        }
    }

    private void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.gameObject.tag == "PointBox")
        {
            isPoint = true;
        }
    }

    private void Rotate() //부드럽게 만들기
    {
        if (Input.GetMouseButtonDown(0))
        {
            for(int i=1; i<91; i++)
            {
                transform.Rotate(0, 0, i);
            }
        }
    }
    private void OnTriggerExit2D(Collider2D other) 
    {
        if (other.gameObject.name == "PointBox")
        {
            isPoint = false;
        }
    }
}

 

pointbox c#스크립트

public class Point : MonoBehaviour
{
    public GameObject PlayerMove;
    private bool istouch = false;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Delete();
    }

    private void Delete()
    {
        if ((Input.GetKeyDown(KeyCode.Space)||Input.GetKeyDown(KeyCode.A)||Input.GetKeyDown(KeyCode.D))&&PlayerMove.GetComponent<PlayerMove>().isPoint&&istouch)
        {
            PlayerMove.GetComponent<PlayerMove>().point++;
            print(PlayerMove.GetComponent<PlayerMove>().point);
            Destroy(this.gameObject);
        }
    }

    private void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.gameObject.name == "Player")
        {
            istouch = true;
        }
    } 

    private void OnTriggerExit2D(Collider2D other) 
    {
        if (other.gameObject.tag == "Player")
        {
            istouch = false;
        }
    }

}

 

'A' 키를 눌러야 할 때에는 빨강색, 'D'는 파랑색으로 지정했다. 

그리고 지난번 방향 전환에서 좀 더 추가해서 'A' 키는 좌우를, 'D'키는 위아래를 움직일 수 있도록 했다.

 

첫번째로 만들 리듬게임의 음악은 '베토벤 바이러스'로 정했다. 그 후 turn 혹은 space바를 눌러야하는 지점에 pointbox를 깔아주었다.

 

Player가 pointbox와 닿았을 때 키를 누르면 pointbox가 사라지도록 하기 위해 OnTriggerEnter2D 함수 안에서 Input을 사용해봤지만 생각대로 작동하지 않았다. 그 이유는 OnTriggerEnter는 1 프레임에만 사용되기 때문이였다. 동일한 프레임(1 프레임에만 해당)에서 키를 눌러야 작동하는 것이었다. 

그래서 bool 변수를 하나 선언해서 trigger 안에 있을 때와 없을 때 각각 OntriggerEnter와 OntriggerExit를 이용해 bool 값을 지정해주었고, pointbox에 적용할 스크립트를 생성해 player에 적용한 스크립트에서 bool 변수를 가져와 pointbox에 닿았을 때 키를 누르면 pointbox가 사라지도록 만들었다.

 

일단 이정도까지 하고 난 뒤 게임을 실행했을 때 하나의 pointbox를 먹으면 모든 pointbox가 사라지는 문제가 발생했다. 그래서 pointbox의 c# 스크립트에서 onTriggerEnter과 onTriggerExit를 이용해 문제를 해결했다.

 

<실행영상>

 

+ Recent posts