void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//마우스의 화면 좌표를 월드 좌표로 변환
transform.position = mousePos;
// 현재 오브젝트의 위치를 마우스 위치로 설정
}
ScreenToWorldPoint는 스크린 좌표를 실제 게임 속 좌표로 변환해준다
public void GameOver()
{
isPlay = false; // 게임 상태를 정지 상태로 전환
anim.SetBool("isDie", true);
Invoke("TimeStop", 0.5f);
nowScore.text = time.ToString("N2");
if (PlayerPrefs.HasKey(key)) // 최고 점수가 있을 때
{
float best = PlayerPrefs.GetFloat(key); // 최고 점수를 가져옴
if(best < time)
{
PlayerPrefs.SetFloat(key, time); // 현재 점수를 최고 점수에 저장
bestScore.text = time.ToString("N2");
}
else
{
bestScore.text = best.ToString("N2");
}
}
else
{
PlayerPrefs.SetFloat(key, time); // 현재 점수를 최고 점수에 저장
bestScore.text = time.ToString("N2");
}
endPanel.SetActive(true);
}
PlayerPref는 로컬에 간단한 데이터를 저장할 수 있는 유니티 내장 클래스
using UnityEngine.SceneManagement;
public class RetryButton : MonoBehaviour
{
public void Retry()
{
SceneManager.LoadScene("MainScene");
// MainScene 이라는 이름의 씬을 다시 불러옴
}
}
SceneManager는 Unity에서 씬을 관리하는 클래스이며, LoadScene은 다른 씬으로 전환하거나 게임을 재시작할 때 사용하는 함수이다
지난 시간에 배운 내용을 바탕으로 마우스 포인터 따라 움직이기와 PlayerPrefs를 활용한 데이터 저장을 구현해보았다.
이미 한 번 해봤던 내용에서 조금 확장된 정도라 큰 어려움 없이 따라갈 수 있었고,
기존 개념들을 더 확실하게 이해하게 된 계기가 되었다.