// 게임개발 종합반 - 3주차(스파르타코딩클럽)
만들 게임 : 고양이 밥주기 게임
- 기본 씬 구성하기 (UI, 강아지, 고양이)
- 강아지 움직임 더하기 + 밥 쏘기
- 고양이 내려오게 하기
- 고양이 밥 먹기 + 옆으로 가게 하기
- 새로운 고양이 나오게 하기
- 레벨업하기
기초 공사
- Main Camera - size 25
- Camera에는 기본적으로 Background 속성이 있음 (FFF0B2)
- gameObject (dog, fishshop) 배치 후 이미지 할당
게임 인트로 만들기
- StartScene 만들기 (Ctrl+N)
- background 넣기
- 게임 스타트 버튼 넣기(UI - Image(startBtn 할당) - Add Conponent(Button)
- startBtn 스크립트 생성 후 On Click 이벤트로 할당
- Button Component 는 UI에만 할당하여 사용 가능
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class startBtn : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GameStart()
{
SceneManager.LoadScene("MainScene");
}
}
- 화면이 가만있으면 심심하니 좌우 rotate anim 넣어보기
밥 쏘기 구현하기
- circle 생성 후 Knob으로 sprite 변경 - prefab으로 저장
- foodShot 위치에서 food를 Instantiate 하도록
public class gameManager : MonoBehaviour
{
public GameObject food;
public GameObject dog;
public GameObject foodShot;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("makeFood", 0, 0.2f);
}
// Update is called once per frame
void Update()
{
}
void makeFood()
{
// foodShot 위치에서 Instantiate
float x = foodShot.transform.position.x;
float y = foodShot.transform.position.y;
Instantiate(food, new Vector3(x, y, 0), Quaternion.identity); // Quaternion.identity 는 회전이 없다는 뜻
}
}
밥 사라지기 구현하기
if (transform.position.y > 26.0f)
{
Destroy(gameObject);
}
dog 움직이기
public class dog : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(mousePos.x, transform.position.y, 0);
}
}
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float x = mousePos.x;
if (x < -10f)
{
x = -10f;
}
else if (x > 10f)
{
x = 10f;
}
transform.position = new Vector3(x, transform.position.y, 0);
}