CameraMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
Transform playerTransform; //게임오브젝트의 위치, 회전 그리고 스케일(scale)을 나타냅니다.
Vector3 Offset; //
void Awake()
{
playerTransform = GameObject.FindWithTag("Player").transform; // Player의 위치를 알려준다.
Offset = transform.position - playerTransform.position; // 카메라의 상대위치를 구한다.
}
void LateUpdate() // LateUpdate를 쓰는 이유는 카메라 이동은 다른 업데이트들이 끝나고 업데이트 되야 따라가는 것처럼 진행되기 때문이다
{
transform.position = playerTransform.position + Offset; // 카메라와 플레이어가 일정한 간격을 두고 떨어지게 한다.
}
}
GameManagerLogic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // 장면 전환 매니저
using UnityEngine.UI; // UI를 가져올수 있는 것
public class GameManagerLogic : MonoBehaviour
{
public int totalItemCount; // 먹어야하는 아이템의 갯수
public int stage; //스테이지 단계
public Text stageCountText; // UI 설정을 위한 변수 화면에 스테이지에서 먹어야하는 아이템의 갯수를 표시함
public Text playerCountText; // UI 설정을 위한 변수 화면에 현쟈 스테이지에서 유저가 획득한 아이템의 갯수를 표시함
void Awake()
{
stageCountText.text = "/ " + totalItemCount.ToString();
}
public void GetItem(int count)
{
playerCountText.text = count.ToString();
}
void OnTriggerEnter(Collider other) //Collider가 다른 트리거 이벤트에 침입했을 때 OnTriggerEnter가 호출됩니다.
{
if (other.gameObject.tag == "Player")
{
SceneManager.LoadScene(stage);
}
}
}
ItemRotate.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemRotate : MonoBehaviour
{
public float rotateSpeed;
void Update()
{
// 먹어야 하는 아이템을 회전 시키는 것 회전축을 기준으로 위쪽으로
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime, Space.World);
}
}
PlayBall.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // 장면 전환 매니저
public class PlayerBall : MonoBehaviour
{
public float jumpPower; // 점프의 힘을 unity에서 바로 조정할 수 있게 public을 줬다.
public int ItemCount; // 먹은아이템
bool isJump; // 점프한 상태
Rigidbody rigid;
AudioSource audio; // 오디오
public GameManagerLogic manager;
void Awake() // Awake함수에서는 선언한 변수들의 초기화를 한다
{
isJump = false;
rigid = GetComponent<Rigidbody>(); // rigidbody가져옴 물리 시뮬레이션을 통해서 오브젝트의 위치를 조절합니다.
// 리지드바디(rigidbody) 컴포넌트는 오브젝트의 위치를 제어합니다.
audio = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetButtonDown("Jump") && isJump == false) // 스페이스바를 누르고 점프를 하고 있지 않는 상태라면
{
isJump = true; // 점프하는 중으로 바꾼다
rigid.AddForce(new Vector3(0, jumpPower, 0), ForceMode.Impulse); // Y축 방향으로 jumpPower만큼 힘을 줘서 올린다.
}
}
void FixedUpdate() // 물리적인 업데이트는 FixedUpdate에서 한다
{
float h = Input.GetAxisRaw("Horizontal"); // 이동을 입력 받아오는 함수 Raw가 붙으면 1값이 들어온다
float v = Input.GetAxisRaw("Vertical"); // 이동을 입력 받아오는 함수 Raw가 붙으면 1값이 들어온다
rigid.AddForce(new Vector3(h, 0, v), ForceMode.Impulse); // 실제로 키보드를 누른 방향으로 힘을 가해서 물체를 이동시키는 함수
}
void OnCollisionEnter(Collision collision) // 점프를 1단 점프로 제한하기 위해 만들었다.
{ // OnCollisionEnter는 이collider/ rigidbody에 다른 collider/ rigidbody가 닿을 때 호출됩니다.
if (collision.gameObject.tag == "Floor") // Floor 태그를 가진 오브젝트와 충돌한다면
{
isJump = false; // 점프하는 상태를 false로 바꿈
}
}
void OnTriggerEnter(Collider other) //item 먹으면 item 사라지기
{ // Find 계열 함수는 CPU의 부하를 초래할 수 있으므로 최소한의 사용만 한다.
if (other.tag == "Item") // Item 태크를 가진 오브젝트와 만나면
{
ItemCount++; // 획득한 아이템의 갯수 증가
audio.Play(); // 오디오 재생
other.gameObject.SetActive(false); //gameObject자기 자신, other을 비활성화 시킨다.
manager.GetItem(ItemCount); // UI표시를 위해 획득한 아이템의 갯수를 전달해준다.
}
else if (other.tag == "Point") // 골인지점을 포인트로 작성함
{
if (manager.totalItemCount == ItemCount) //모든 아이템을 먹고 골인할 경우
{
//clear
SceneManager.LoadScene("stage_" + (manager.stage + 1).ToString());
}
else // 모든 아이템을 먹지 않고 골인할 경우
{
// restart
SceneManager.LoadScene("stage_" + manager.stage.ToString());
}
}
}
}
'공부 > 기타' 카테고리의 다른 글
백엔드 진화과정 (0) | 2024.08.09 |
---|---|
RESTful API (1) | 2024.08.09 |
객체지향 SOLID (0) | 2024.08.09 |
개발 프로세스 (0) | 2024.08.09 |