using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UIStartMain : MonoBehaviour
{
[SerializeField] Button startBtn;
void Start()
{
this.startBtn.onClick.AddListener(() => {
SceneManager.LoadScene(1); // 다음 게임씬으로 이동
});
}
}
3. UIStartMain.cs
using System.Collections;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
public float speed = 5.0f; // 캐릭터 이동 속도
private Coroutine currentMoveCoroutine; // 현재 실행 중인 이동 코루틴의 참조
void Start()
{
StartCoroutine(CoMove());
}
IEnumerator CoMove()
{
while (true)
{
if (Input.GetMouseButtonDown(0)) // 마우스 왼쪽 버튼 클릭
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) // 레이캐스트가 어떤 오브젝트에 닿았는지 확인
{
Vector3 targetPosition = hit.point; // 클릭된 위치
targetPosition.y = transform.position.y; // 캐릭터의 높이 유지
if (currentMoveCoroutine != null)
{
StopCoroutine(currentMoveCoroutine); // 현재 진행 중인 코루틴 중지
}
currentMoveCoroutine = StartCoroutine(MoveForward(targetPosition)); // 새 위치로 이동 시작
}
}
yield return null;
}
}
IEnumerator MoveForward(Vector3 targetPosition)
{
transform.LookAt(targetPosition); // 캐릭터가 목표 위치를 바라보게 함
while (Vector3.Distance(transform.position, targetPosition) > 0.05f) // 목표 위치에 가까워질 때까지
{
Vector3 direction = (targetPosition - transform.position).normalized; // 이동 방향
transform.position += direction * speed * Time.deltaTime; // 캐릭터 이동
yield return null;
}
}
}
4. CharacterController.cs
반응형
'산대특 > 게임 UIUX프로그래밍' 카테고리의 다른 글
[LearnUGUI] 인벤토리 만들기 (1) - Grid Scroll VIew 만들기 (0) | 2024.02.18 |
---|---|
[LearnUGUI] 동적 Gem ScrollView + 데이터 연동 구현하기 (0) | 2024.02.11 |
[LearnUGUI] Home 화면에서 광고 버튼 누르면 Popup 보여주기 (0) | 2024.02.10 |
[LearnUGUI] Settings 화면 만들기 (0) | 2024.02.10 |
[LearnUGUI] ShopChest 동적 스크롤 뷰 + 데이터 연동 구현 (0) | 2024.02.08 |