새소식

C#,Unity Programming

게임개발 학습. 고양이 밥주기 게임 (Scene 전환/ 총 쏘기/ 마우스 좌우 값 제한)

  • -
728x90

 

// 게임개발 종합반 - 3주차(스파르타코딩클럽)

만들 게임 : 고양이 밥주기 게임

  1. 기본 씬 구성하기 (UI, 강아지, 고양이)
  2. 강아지 움직임 더하기 + 밥 쏘기
  3. 고양이 내려오게 하기
  4. 고양이 밥 먹기 + 옆으로 가게 하기
  5. 새로운 고양이 나오게 하기
  6. 레벨업하기

기초 공사

  • 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 는 회전이 없다는 뜻
    }
}

밥 사라지기 구현하기

  • food.cs
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);
    }
}
  • x값 좌우 제한
    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);
        
    }
Contents
  • -

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.