1. 게임 계획
- 게임이름 : 굴러서 아이템 먹기
- 장르 : 캐주얼 액션
- 목표 : 지형을 뛰어넘어 굴러서 아이템을 먹고 목표지점에 도달
- 구성 : 공(Player), 아이템(Item), 지형(Platform), 결승점(Point)
2. 플레이어
공 움직임, 점프 구현하기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBall : MonoBehaviour
{
public float jumpPower;
bool isJump;
Rigidbody rigid;
void Awake()
{
isJump = false;
rigid = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButtonDown("Jump") && !isJump)
{
isJump = true;
rigid.AddForce(new Vector3(0, jumpPower, 0), ForceMode.Impulse);
}
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
rigid.AddForce(new Vector3(h, 0, v), ForceMode.Impulse);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Floor")
isJump = false;
}
}
3. 아이템
제자리 회전
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemCan : MonoBehaviour
{
public float rotateSpeed;
void Update()
{
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime, Space.World);
}
}
플레이어가 아이템 건들면 사라지고 count
음악 컴포넌트 추가
> AudioSource : 사운드 재생 컴포넌트로 AudioClip(사운드 파일 컴포넌트) 필요
Tag : 오브젝트를 구분하는 단순한 문자열
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBall : MonoBehaviour
{
public float jumpPower;
public int itemCount;
AudioSource audio;
bool isJump;
Rigidbody rigid;
void Awake()
{
isJump = false;
rigid = GetComponent<Rigidbody>();
audio = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetButtonDown("Jump") && !isJump)
{
isJump = true;
rigid.AddForce(new Vector3(0, jumpPower, 0), ForceMode.Impulse);
}
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
rigid.AddForce(new Vector3(h, 0, v), ForceMode.Impulse);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Floor")
isJump = false;
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "Item")
{
itemCount++;
audio.Play();
//gameObject : 자기자신
other.gameObject.SetActive(false); //SetActive(bool) : 오브젝트 활성화 함수
}
}
}
4. 카메라
카메라가 움직이는 공을 따라서 움직이도록 한다.
> 메인카메라에 넣는 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
Transform playerTransform;
Vector3 Offset;
void Awake()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
Offset = transform.position - playerTransform.position;
}
void LateUpdate()
{
transform.position = playerTransform.position + Offset;
}
}
5. 결승점(Finish Point)
6. 장면 이동
>> 형태가 없고 전반적인 로직을 가진 오브젝트를 매니저로 지정
이 게임에서는 아이템의 개수를 저장하는 것으로 사용됨.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManagerLogic : MonoBehaviour
{
public int TotalItemCount;
public int stage;
}
SceneManager : 장면을 관리하는 기본 클래스
이때! 중요한 점은 Scene을 불러오기위해서는 꼭 Build Setting에서 추가해주어야 한다!!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // Scene 가져오기 위해 꼭 필요!
public class PlayerBall : MonoBehaviour
{
public float jumpPower;
public int itemCount;
public GameManagerLogic manager;
bool isJump;
AudioSource audio;
Rigidbody rigid;
void Awake()
{
isJump = false;
rigid = GetComponent<Rigidbody>();
audio = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetButtonDown("Jump") && !isJump)
{
isJump = true;
rigid.AddForce(new Vector3(0, jumpPower, 0), ForceMode.Impulse);
}
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
rigid.AddForce(new Vector3(h, 0, v), ForceMode.Impulse);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Floor")
isJump = false;
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "Item")
{
itemCount++;
audio.Play();
//gameObject : 자기자신
other.gameObject.SetActive(false); //SetActive(bool) : 오브젝트 활성화 함수
manager.GetItem(itemCount);
}
// Finish Point
else if(other.tag == "Finish")
{
//Scene을 불러오려면 꼭 Build Setting에서 추가!
if(itemCount == manager.TotalItemCount)
{
//Game Clear! && Next Stage
if(manager.stage == 2)
SceneManager.LoadScene(0);
else
SceneManager.LoadScene(manager.stage + 1);
}
else
{
//ReStart..
//SceneManager : 장면을 관리하는 기본 클래스
SceneManager.LoadScene(manager.stage); //LoadScene() : 주어진 장면을 불러오는 함수
}
}
}
}
8. 유저 인터페이스
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManagerLogic : MonoBehaviour
{
public int TotalItemCount;
public int stage;
public Text stageCountText;
public Text playerCountText;
void Awake()
{
stageCountText.text = "/ " + TotalItemCount;
}
public void GetItem(int count)
{
playerCountText.text = count.ToString();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
SceneManager.LoadScene(stage);
}
}
'Unity' 카테고리의 다른 글
유니티 게임 인터페이스 (0) | 2021.08.10 |
---|---|
유니티 물리 충돌 이벤트 (0) | 2021.08.09 |
유니티 힘을 이용하여 물체 움직여보기 (0) | 2021.08.09 |
유니티 실제와 같은 물체 만들기(중력 적용, 충돌, 재질 만들기) (0) | 2021.08.09 |
유니티 deltaTime (0) | 2021.08.08 |