C#/수업내용

0708 슈팅게임

버스창문 2022. 7. 8. 12:52

저기있는 length가 애니메이션 최대 길이
이펙트를 애니메이션으로 만들일이 있다면 저 시간을 참조해서 destroy 하면됨

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMain : MonoBehaviour
{
    public EnemyGenerator enemyGenerator;
    public UIGame uIGame;
    private EnemyControler enemy;
    private int score = 0;
    // Start is called before the first frame update
    void Start()
    {
        
        this.enemyGenerator.enemyCreate = (enemyGo) =>
        {
            if (this.enemy == null)
            {
                Debug.Log("적생성");
                this.enemy = Instantiate(enemyGo, new Vector3(Random.Range(-2, 3), Random.Range(-2, 3), 0), Quaternion.identity);
                Debug.Log(enemyGo);
                this.enemy.DieEvent = () =>
                {
                    this.score += this.enemy.score;
                    this.uIGame.score.text = this.score + "점";
                };
            }

        };
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

this.enemy를 만들지 않은채 enemyGo로 생성하고 dieEvent를 넣으려했지만 작동하지 않음
this.enemy를 만들어서 생성한다음 할당시켜서 this.enemy를 이용해서 이벤트를 넣어줘야함
코드의 의미는
1. enemyGenerator에 있는 enemyControler를 포함하는 게임 오브젝트를 매개변수로 받는 enemyCreate 액션에

2. this.enemy가 null일때 매개변수 enemyGo를 생성해서 this.enemy에 할당하고
3.this.enemy에 있는 dieEvent에

4. this.score를 this.enemy.score만큼 더하고
5. ui에 띄운다는 내용을
6.대리자로 전달하는 내용을
7.대리자로 전달

 

 

 

 

 

 

 

선생님이 만든방법 
1. player에 애니메이션 (idle)

2. shipcontroler에서

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShipControler : MonoBehaviour
{
    [SerializeField]
    private GameObject bullet;
    [SerializeField]
    private float speed = 1.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        var movement = new Vector3(1, 0, 0) * Time.deltaTime * this.speed;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var shot = Instantiate(this.bullet, this.transform.position + new Vector3(0, 0.66f, 0),Quaternion.identity);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            this.transform.position -= movement;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            this.transform.position += movement;
        }
    }
}

나는 movement를 만들어서 
position에서 빼고 더하는 식으로 만듬(좌우만 만든 이유는 맨처음 만들때 좌우만 만들라고 하셨음)
선생님은 input.GetAxisRaw("horizontal") (// 왼쪽누르면 -1 반환, 오른쪽누르면 1반환) 을 만들고 그 후 Vertical도 만들어서 new Vector(h,v,0)을 만든다음, 이걸 그냥 하면 대각선 이동할때 빨라지므로 단위 벡터로 만들어서 Translate에 넣음

 

 

총알 쏘는법

나는 총알을 생성할때 배의 좌표에서 y축으로 + 0.66f된 좌표로 생성했음
선생님은 배에 자식오브젝트를 생성하고 거기서 총알 발사되도록 만듬

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BullrtControler : MonoBehaviour
{
    public float speed = 1f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        var movement = new Vector3(0, 1, 0)/*vector3.up*/ * this.speed * Time.deltaTime;
        //this.transform.Translate(0, 1 * this.speed * Time.deltaTime, 0) ;
        this.transform.Translate(movement);
        if(this.transform.position.y > 5.5f)
        {
            Destroy(this.gameObject);
        }
    }
}

bulletcontroler
똑같이 만듬

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class EnemyControler : MonoBehaviour
{
    public int score = 60;
    public UnityAction DieEvent;
    public GameObject explosionGo;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "bullet")
        {
            Destroy(collision.gameObject);
            this.DieEvent();
            Instantiate(this.explosionGo, this.transform.position, Quaternion.identity);
            Destroy(this.gameObject);
        }
    }
}

enemyControler

나는 rigidbody붙일때 적한태 붙여놨지만 선생님은 총알에 붙임
또한 나는 충돌판정 만들때 bullet에만 bullet테그를 만들어 넣고 확인하는걸 적한태 넣었음

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class EnemyGenerator : MonoBehaviour
{
    [SerializeField]
    private EnemyControler enemyGo;
    public UnityAction<EnemyControler> enemyCreate;
    // Start is called before the first frame update
    void Start()
    {
        //Instantiate(this.enemyGo);
    }

    // Update is called once per frame
    void Update()
    {
        /*if(this.enemy == null)
        {
            *//*this.enemyCreate(this.enemyGo);*/
        /*this.enemy = Instantiate(this.enemyGo, new Vector3(Random.Range(-3, 3), Random.Range(-3, 3), 0),Quaternion.identity);*//*
    }*/
        this.enemyCreate(this.enemyGo);
    }
}

나는 enemyGenerator가 하는일이 enemy정보 받아오기 enemyCreate대리자 호출밖에 없음

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMain : MonoBehaviour
{
    public EnemyGenerator enemyGenerator;
    public UIGame uIGame;
    private EnemyControler enemy;
    private int score = 0;
    // Start is called before the first frame update
    void Start()
    {
        
        this.enemyGenerator.enemyCreate = (enemyGo) =>
        {
            if (this.enemy == null)
            {
                Debug.Log("적생성");
                this.enemy = Instantiate(enemyGo, new Vector3(Random.Range(-2, 3), Random.Range(-2, 3), 0), Quaternion.identity);
                Debug.Log(enemyGo);
                this.enemy.DieEvent = () =>
                {
                    this.score += this.enemy.score;
                    this.uIGame.score.text = this.score + "점";
                };
            }

        };
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

gameMain 적 생성하고 UI관리하는 일을 전부 얘가함
나는 얘한태 UI, enemyGenerator만 전달했지만 선생님은 player도 일단 전달시킴
나는 아예 모든 일을 gameMain에서 대리자로 전달했지만 선생님은 Generate에 create매서드 생성후 그걸 main에서 호출함

 

나는 죽었을때 재생성 되는걸 main에서 enemy가 null인지 확인해서 넣었지만 선생님은 enemy에 explode매서드를 생성해서 거기서 destroy(this.gameObject)를 넣고 generator에서  enemy에 있는 onExplode 액션에 create매서드를 재귀함수로 생성함   

 

 

'C# > 수업내용' 카테고리의 다른 글

0718  (0) 2022.07.18
0708 오브젝트 풀링  (0) 2022.07.08
0707 유니티  (0) 2022.07.07
0707 표창날리기  (0) 2022.07.07
0707 swipeCar  (0) 2022.07.07