728x90
반응형
생각해보니 맵에 다리를 만들어 두고 떨어지면 어떻게 할 지 생각을 안했습니다.
그래서 플레이어가 떨어지면 리셋이 되도록 만들기로 했습니다.
카메라 밑에 검이랑 플레이어용 캡슐이 있었는데 캡슐에 코드를 넣어 주었습니다.
ResetSpawn.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResetSpawn : MonoBehaviour
{
public PlayerController playerController;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("ResetGround"))
{
Debug.Log(1);
Camera.main.transform.position = new Vector3(0, 3.7f, -7.4f);
Debug.Log(2);
if (playerController != null)
{
playerController.isResetting = true;
}
StartCoroutine(ResetPlayer(playerController));
}
}
private IEnumerator ResetPlayer(PlayerController playerController)
{
yield return new WaitForSeconds(0.1f);
playerController.isResetting = false;
}
}
(Debug는 실행 확인용입니다.)
그 다음 플레이어 컨트롤러에서 isResetting을 추가한 뒤, isResetting이 true라면 움직이지 못하도록 만들었습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float mouseSensitivity = 100f;
public float jumpHeight = 2f;
private float xRotation = 0f;
private CharacterController controller;
private Vector3 velocity;
private float gravity = -9.81f;
public bool isResetting = false;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController>();
}
void Update()
{
if (isResetting) return;
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
float yRotation = transform.eulerAngles.y + mouseX;
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0f);
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
Vector3 move = transform.right * moveX + transform.forward * moveZ;
controller.Move(move * moveSpeed * Time.deltaTime);
if (controller.isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
실수로 화면을 작게하고 찍었네요. 마지막에 보면 바닥에 떨어졌을 때 플레이어가 리셋이 되는 것을 볼 수 있습니다.
아까 확인용이였던 Debug도 찍히네요.
'Unity' 카테고리의 다른 글
| 3D 테스트 프로젝트 6 (0) | 2025.02.27 |
|---|---|
| 3D 테스트 프로젝트 5 (0) | 2025.02.26 |
| 3D 테스트 프로젝트 3 (0) | 2025.02.24 |
| 3D 테스트 프로젝트 2 (0) | 2025.02.23 |
| 3D 테스트 프로젝트 1 (0) | 2025.02.22 |