시점을 변경하고 자동차에 콜라이더를 넣어서

점의 레이캐스트가 닿을 시 인지할 수 있도록 하였다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.LowLevel;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private Coroutine coroutine;

    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.Log("Hover");
    }


    public void OnPointerEnter(PointerEventData eventData)
    {
        coroutine = StartCoroutine(CoClick());
        Debug.Log("Enter");
    }


    private IEnumerator CoClick()
    {
        float delta = 0f;
        while (true)
        {
            delta += Time.deltaTime;
            if(delta >= 3)
            {
                Debug.LogFormat("Clicked , {0}", delta);
                delta = 0f;
            }
            yield return null;
        }
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Exit");
        StopCoroutine(coroutine);
    }
}

 

 

플레이어가 지정한 지정한 위치에 도달하면 방향을 전환하여

다음 목적지를 향해 이동하고 그에 맞춰 VR의 시선이 달라진다.

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

public class Player : MonoBehaviour
{
    public Transform[] wayPoints;
    private int nextIdx = 0;
    private float speed = 2f;
    void Start()
    {
        
    }

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

    public void MoveWayPoint()
    {
        Vector3 dir = wayPoints[nextIdx].position - this.transform.position;
        // 회전 각도
        Quaternion rot = Quaternion.LookRotation(dir);

        this.transform.Translate(Vector3.forward * speed * Time.deltaTime);

        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, rot, Time.deltaTime * speed);

    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("WayPoint"))
        {
            nextIdx++;
            if(nextIdx == 4)
            {
                nextIdx = 0;
            }
        }
    }



}

 

'산대특 > VRAR' 카테고리의 다른 글

Oculus Settings and Grab  (0) 2024.04.17
Reticle  (0) 2024.04.16
VR로 360도 동영상 다운로드 후 적용하기 + 오큘러스 적용  (0) 2024.04.16
VR Setting 문제  (0) 2024.04.12
VR Setting 문제  (0) 2024.04.12

 

 

 

 

아이템을 만들것이다.

스프라이트를 나눠서 가져온다.

총알처럼 콜라이더와 리지드바디 추가 + 이즈트리거 체

이때 중력은 0

아이템 스크립트 생성후 3개에 부착

item 스크립트에 퍼블릭으로 string으로 선언하고 각 아이템의 이름을 직접 적어줄 것이다.

물체가 잘 내려오는지 확인

이제 애니메이션을 넣어줄것이다.

다른 오브젝트들도 똑같이 애니메이션을 만든다.

테스트

아이템 태그 부착

 

플레이어가 아이템을 먹어야하기에 onTriggerEnter2D를 사용할 것이다.

그런데 파워는 이미 선언되어 있고

파워 아이템을 먹으면 공격력이 증가되어야 하기에 변수를 따로 하나 더 선언

파워가 맥스 파워보다 증가되면 안되므로 조건을 걸어주고

맥스파워가 되면 보통 슈팅게임처럼 점수만 올려줄 것이다.

이제 Boom을 만들 것이다.

 

붐스킬 이미지를 가져오면 다른 오브젝트를 덮어버리는데

이는 order in layer로 층을 바꾸면 된다

현재 모두 0 이고 배경은 -1 이므로

배경은 -2로 붐스킬은 -1로 변경

 

그 후 붐스킬도 똑같이 애니메이션을 만들어 준다.

 

나중에 아이템 먹을 시 활성화 할것이므로 현재는 비활성화

 

그럼 플레이에게 따로 값을 주면된다.

 

플레이에게 퍼블릭으로 인스턴스를 만들어주고 어싸인

Boom을 먹으면 스킬을 활성화 하고 평상시에는 4초후 이펙트가 꺼지게 설정

적들은 배열로 관리하여 배열에서 찾아서 제거해준다.

퍼블릭으로 변경

플레이어의 파워와 맥스파워 설정

 보통 슈팅게임에서 아이템을 먹으면 저장을 하고

원하는 시기에 폭탄을 쏠 수 있도록 조정해야 한다.

그러므로 폭탄 개수를 저장할 변수 선언

폭탄도 파워와 같이 한계를 정할 것이므로 maxBoom까지 두개를 만들 것이다.

이제 Boom메소드를 만들것인데

아까 구현해던

이것을 다 긁어서 붐 메소드 안에 넣어줄 것이다.

붐 메서드에 기능은 이제 따로 빼두었는데

붐도 파워처럼 먹었을때에 대한 설정을 다시 지정해준다.

 

초기화

붐 아이콘도 라이프 아이콘을 복사해서 앵커의 위치를 바꾸어 준다.

게임 매니저에서 라이프 아이콘을 관리했던 것 처럼 복사해서

붐으로 만든 메소드에 집어 넣는다.

플레이어에서 게임매니저를 가져오고 업데이트하기 위해 메소드를 넣어준다.

알베도의 값을 0으로 줄인다.

만들어진 아이템 프리팹화

에너미가 사라진 곳에 아이템이 생성될 것이다.

어싸인

플레이 해보면 하나에서 아이템 2개가 나오는 오류?가 생긴다.

따라서 예외처리해야하는데

코드를 잘 살펴보면 디스트로이 되기 전에 온히트가 2번이라는 것을 알 수 있다.

이 코드만 추가해주면 예외처리가 된다.

 

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Net.Http.Headers;
using UnityEngine;

public class Player : MonoBehaviour
{
    public bool isTouchTop;
    public bool isTouchBottom;
    public bool isTouchRight;
    public bool isTouchLeft;

    public int life;
    public int score;
    public int speed;
    public int power;
    public int boom;
    public int maxBoom;
    public float maxPower;
    public float maxShotDelay; //실제 딜레이
    public float curShorDelay; //한발 쏜 후 딜레이
    
    public GameObject bulletObjA;
    public GameObject bulletObjB;
    public GameObject boomSkill;

    public GameManager manager;
    public bool isHit;
    public bool isBoomTime;

    private Animator anim;

    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    void Move()
    {
        float h = Input.GetAxisRaw("Horizontal");
        if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
            h = 0;
        float v = Input.GetAxisRaw("Vertical");
        if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
            v = 0;
        Vector3 curPos = this.transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
        transform.position = curPos + nextPos;

        if (Input.GetButtonDown("Horizontal") ||
            Input.GetButtonDown("Vertical"))
        {
            anim.SetInteger("Input", (int)h);
        }
    }

    private void Update()
    {
        Move();
        Fire();
        Boom();
        Reload();
    }

    void Fire()
    {
        if (!Input.GetButton("Fire1"))
            return;
        if (curShorDelay < maxShotDelay)
            return;

        switch (power)
        {

            case 1://Power One
                GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
                Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
                rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                break;
            case 2:
                GameObject bulletR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.1f, transform.rotation);
                GameObject bulletL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.1f, transform.rotation);
                Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
                rigidR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
            case 3:
                GameObject bulletRR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.3f, transform.rotation);
                GameObject bulletCC = Instantiate(bulletObjB, transform.position, transform.rotation);
                GameObject bulletLL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.3f, transform.rotation);
                Rigidbody2D rigidRR = bulletRR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidCC = bulletCC.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidLL = bulletLL.GetComponent<Rigidbody2D>();
                rigidRR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidCC.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidLL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
        }
                curShorDelay = 0;
    }

    void Reload()
    {
        curShorDelay += Time.deltaTime;

    }
    
    void Boom()
    {
        if (!Input.GetKeyDown(KeyCode.Space))
            return;
        if (boom == 0)
            return;
        boom--;
        isBoomTime = true;
        manager.UpdateBoomIcon(boom);

        if (isBoomTime)
        {
            boomSkill.SetActive(true);
            Invoke("OffBoomSkill", 4f);
            GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
            for (int index = 0; index < enemies.Length; index++)
            {
                Enemy enemyLogic = enemies[index].GetComponent<Enemy>();
                enemyLogic.OnHit(1000);
            }
            GameObject[] bullets = GameObject.FindGameObjectsWithTag("EnemyBullet");
            for (int index = 0; index < bullets.Length; index++)
            {
                Destroy(bullets[index]);
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Border")
        {
            switch(collision.gameObject.name)
            {
                case "Top":
                    isTouchTop = true;
                    break;
                case "Bottom":
                    isTouchBottom = true;
                    break;
                case "Right":
                    isTouchRight = true;
                    break;
                case "Left":
                    isTouchLeft = true;
                    break;
            }
        }
        else if(collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyBullet")
        {
            if (isHit)
            {
                return;
            }
            isHit = true;
            life--;
            manager.UpdateLifeIcon(life);

            if(life == 0)
            {
                manager.GameOver();
            }
            else
            {
                manager.RespawnPlayer();
            }
            gameObject.SetActive(false);
        }
        else if(collision.gameObject.tag == "Item")
        {
            Item item = collision.gameObject.GetComponent<Item>();
            switch(item.type)
            {
                case "Coin":
                    score += 1000;
                    break;
                case "Power":
                    if(power == maxPower)
                    {
                        score += 500;
                    }
                    else
                    {
                        power++;
                    }
                    break;
                case "Boom":
                    if (boom == maxBoom)
                    {
                        score += 500;
                        
                    }
                    else
                        boom++;
                        manager.UpdateBoomIcon(boom);
                    break;
            }
            Destroy(collision.gameObject);
        }
    }

    void OffBoomSkill()
    {
        boomSkill.SetActive(false);
        isBoomTime = false;
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        switch (collision.gameObject.name)
        {
            case "Top":
                isTouchTop = false;
                break;
            case "Bottom":
                isTouchBottom = false;
                break;
            case "Right":
                isTouchRight = false;
                break;
            case "Left":
                isTouchLeft = false;
                break;
        }
    }
}

 

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

public class Bullet : MonoBehaviour
{
    public int dmg;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BorderBullet")
        Destroy(gameObject); //모노비헤이어에 들어 있는 자체 함수 => 매개변수 오브젝트를 삭제하는 함수
    }
}

 

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public string enemyName;
    public int enemyScore;
    public float speed;
    public int health;
    public Sprite[] sprites;

    public float maxShotDelay; //실제 딜레이
    public float curShorDelay; //한발 쏜 후 딜레이

    public GameObject bulletObjA;
    public GameObject bulletObjB;
    public GameObject itemCoin;
    public GameObject itemPower;
    public GameObject itemBoom;
    public GameObject player;

    SpriteRenderer spriteRenderer;
    Rigidbody2D rigid;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        rigid = GetComponent<Rigidbody2D>();
        rigid.velocity = Vector2.down * speed;
    }
    private void Update()
    {
        Fire();
        Reload();
    }
    void Fire()
    {
        if (curShorDelay < maxShotDelay)
            return;

        if (enemyName == "S")
        {
            GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
            Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
            Vector3 dirVec = player.transform.position - transform.position;
            rigid.AddForce(dirVec.normalized * 3, ForceMode2D.Impulse);  // 방향 벡터를 정규화하여 힘을 가하는 부분 수정
        }
        else if (enemyName == "L")
        {
            GameObject bulletR = Instantiate(bulletObjB, transform.position + Vector3.right * 0.3f, transform.rotation);
            GameObject bulletL = Instantiate(bulletObjB, transform.position + Vector3.left * 0.3f, transform.rotation);
            Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
            Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
            Vector3 dirVecR = player.transform.position - (transform.position + Vector3.right * 0.3f);
            Vector3 dirVecL = player.transform.position - (transform.position + Vector3.left * 0.3f);
            rigidR.AddForce(dirVecR.normalized * 4, ForceMode2D.Impulse);  // 방향 벡터를 정규화하여 힘을 가하는 부분 수정
            rigidL.AddForce(dirVecL.normalized * 4, ForceMode2D.Impulse);  // 방향 벡터를 정규화하여 힘을 가하는 부분 수정
        }

        curShorDelay = 0;  // 발사 후 딜레이 초기화는 발사 이후에 처리되도록 이동
    }

    void Reload()
    {
        curShorDelay += Time.deltaTime;

    }
    public void OnHit(int dmg)
    {
        if (health <= 0)
            return;
        health -= dmg;
        spriteRenderer.sprite = sprites[1]; //피격시 1번
        Invoke("ReturnSprite", 0.1f);
        if (health <= 0)
        {
            Player playerLogic = player.GetComponent<Player>();
            playerLogic.score += enemyScore;

            //#.Random.Ratio Item Drop
            int ran = Random.Range(0, 10);
            if (ran < 3)
            {
                Debug.Log("No Item");
            }
            else if (ran < 6) { //Coin 30%
                Instantiate(itemCoin, transform.position, itemCoin.transform.rotation);
            }else if (ran < 8) { //Power 20%
                Instantiate(itemPower, transform.position, itemPower.transform.rotation);
            }else if(ran < 10) { //Boom 20%
                Instantiate(itemBoom, transform.position, itemBoom.transform.rotation);
            }



            Destroy(gameObject);
        }
    }

    void ReturnSprite()
    {
        spriteRenderer.sprite = sprites[0];
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "BorderBullet")
        {
            Destroy(gameObject );
        }
        else if(collision.gameObject.tag == "PlayerBullet")
        {
            Bullet bullet = collision.gameObject.GetComponent<Bullet>();
            OnHit(bullet.dmg);
            Destroy(collision.gameObject);
        }
    }

}

 

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject[] enemyObjs;
    public Transform[] spawnPoints;

    public float maxSpawnDelay;
    public float curSpawnDelay;

    public GameObject player;
    public Text scoreText;
    public Image[] lifeImage;
    public Image[] boomImage;
    public GameObject gameOverSet;
    private void Update()
    {
        curSpawnDelay += Time.deltaTime;

        if (curSpawnDelay > maxSpawnDelay)
        {
            SpawnEnemy();
            maxSpawnDelay = Random.Range(0.5f, 3f);
            curSpawnDelay = 0;
        }

        //UI Score

        Player playerLogic = player.GetComponent<Player>();
        scoreText.text = string.Format("{0:n0}", playerLogic.score);



    }
    void SpawnEnemy()
    {
        int ranEnemy = Random.Range(0, 3); //소환될 적
        int ranPoint = Random.Range(0, 9); //소환될 위치
        GameObject enemy = Instantiate(enemyObjs[ranEnemy],
                                        spawnPoints[ranPoint].position,
                                        spawnPoints[ranPoint].rotation);
        Rigidbody2D rigid = enemy.GetComponent<Rigidbody2D>();
        //스피드도 가지고 와야한다.
        Enemy enemyLogic = enemy.GetComponent<Enemy>();
        //적비행기 생성 직후 플레이에 변수를 넘겨준다.
        enemyLogic.player = player;
        if (ranPoint == 5 || ranPoint == 6)
        {
            enemy.transform.Rotate(Vector3.back * 90); //z축으로 90도
            rigid.velocity = new Vector2(enemyLogic.speed * (-1), -1);
        }else if(ranPoint == 7 || ranPoint == 8)
        {
            enemy.transform.Rotate(Vector3.forward * 90); //z축으로 90도
            rigid.velocity = new Vector2(enemyLogic.speed, -1);
        }
        else
        {
            rigid.velocity = new Vector2(0, enemyLogic.speed * (-1));
        }
    }

    public void UpdateLifeIcon(int life)
    {
        //UI Init Disable
        for (int index = 0; index < 3; index++)
        {
            lifeImage[index].color = new Color(1, 1, 1, 0);
        }
        //UI Life Active
        for (int index = 0; index < life; index++)
        {
            lifeImage[index].color = new Color(1, 1, 1, 1);
        }
    }


    public void UpdateBoomIcon(int boom)
    {
        //UI Init Disable
        for (int index = 0; index < 3; index++)
        {
            boomImage[index].color = new Color(1, 1, 1, 0);
        }
        //UI Life Active
        for (int index = 0; index < boom; index++)
        {
            boomImage[index].color = new Color(1, 1, 1, 1);
        }
    }

    public void RespawnPlayer()
    {
        Invoke("RespawnPlayerExe", 2f);
        
    }
    private void RespawnPlayerExe()
    {
        player.transform.position = Vector3.down * 3.5f;
        player.SetActive(true);

        Player playerLogic = player.GetComponent<Player>();
        playerLogic.isHit = false;
    }
    public void GameOver()
    {
        gameOverSet.SetActive(true);
    }

    public void GameRetry()
    {
        SceneManager.LoadScene(0);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
    public string type;
    private Rigidbody2D rigid;


    void Start()
    {
        rigid = GetComponent<Rigidbody2D>();
        rigid.velocity = Vector3.down * 3f; //아이템이 떨어지는 속도
    }

    void Update()
    {
        
    }
}

 

 

리스폰 포인트 추가

추가후 어싸인

 

이제 적들이 옆에서도 등장하므로 위에서 아래로 떨어지는 부분 삭제

 

적 비행기 속도를 게임매니저가 관리할 것이다.

 

1, 2, 3, 4포인트는 x축은 변함이 없을 것이고 y축은 -1로 고정하여 아래로 내려오게

5, 6 포인트는 왼쪽(-1), y축은 -1

7, 8 포인트는 오른쪽(1), y축은 -1

생성시 보더불릿에 닿으면 사라져서 생성되지 않으니 위치 재조정

그림과 같이 옆모양으로 나오게 하기 위해 회전을 추가

 

이제 적의 총알을 만들 것이다.

기존의 불렛b를 복사한후 프리팹모드에 들어가서 이름을 바꾸고 저장

스프라이트 변경후 박스콜라이더 크기 재설정

 

하나더 만들어준 후 태그 달기

플레이어에 있는 코드를 가져와서 부착

네임은 오브젝트네임 자체에 들어가 있으므로 이름 변경

적이 생성될때 플레이어를 알아야한다.

적 비행기 생성 직후 플레이어 변수를 넘겨준다.

목표물 방향 = 목표물위치 - 자신의 위치

큰 적은 두발을 쏜다.

플레이어는 인스턴스화가 되어있지 않기에 할당X

 

 if문에 총알을 발사할 이름을 넣었기 때문에 이름을 할당

 

적들의 총알이 너무 빠르므로 속도를 조절

 

플레이어의 온트리거에 추가

게임매니저가 플레이러를 리스폰 시킬수 있다.

 

플레이어도 게임매니저를 알아야한다.

 public GameManager manager;

 

2초 후에 플레이어가 부활하게 할 것이다.

플레이어 스크립트에 리스폰 추가

 

using System.Collections;
using System.Collections.Generic;
using System.Net.Http.Headers;
using UnityEngine;

public class Player : MonoBehaviour
{
    public bool isTouchTop;
    public bool isTouchBottom;
    public bool isTouchRight;
    public bool isTouchLeft;
    private Animator anim;

    public float speed;
    public float power;
    public float maxShotDelay; //실제 딜레이
    public float curShorDelay; //한발 쏜 후 딜레이

    public GameObject bulletObjA;
    public GameObject bulletObjB;

    public GameManager manager;
    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    void Move()
    {
        float h = Input.GetAxisRaw("Horizontal");
        if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
            h = 0;
        float v = Input.GetAxisRaw("Vertical");
        if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
            v = 0;
        Vector3 curPos = this.transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
        transform.position = curPos + nextPos;

        if (Input.GetButtonDown("Horizontal") ||
            Input.GetButtonDown("Vertical"))
        {
            anim.SetInteger("Input", (int)h);
        }
    }

    private void Update()
    {
        Move();
        Fire();
        Reload();
    }

    void Fire()
    {
        if (!Input.GetButton("Fire1"))
            return;
        if (curShorDelay < maxShotDelay)
            return;

        switch (power)
        {

            case 1://Power One
                GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
                Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
                rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                break;
            case 2:
                GameObject bulletR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.1f, transform.rotation);
                GameObject bulletL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.1f, transform.rotation);
                Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
                rigidR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
            case 3:
                GameObject bulletRR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.3f, transform.rotation);
                GameObject bulletCC = Instantiate(bulletObjB, transform.position, transform.rotation);
                GameObject bulletLL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.3f, transform.rotation);
                Rigidbody2D rigidRR = bulletRR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidCC = bulletCC.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidLL = bulletLL.GetComponent<Rigidbody2D>();
                rigidRR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidCC.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidLL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
        }
                curShorDelay = 0;
    }

    void Reload()
    {
        curShorDelay += Time.deltaTime;

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Border")
        {
            switch(collision.gameObject.name)
            {
                case "Top":
                    isTouchTop = true;
                    break;
                case "Bottom":
                    isTouchBottom = true;
                    break;
                case "Right":
                    isTouchRight = true;
                    break;
                case "Left":
                    isTouchLeft = true;
                    break;
            }
        }
        else if(collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyBullet")
        {
            manager.RespawnPlayer();
            gameObject.SetActive(false);

        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        switch (collision.gameObject.name)
        {
            case "Top":
                isTouchTop = false;
                break;
            case "Bottom":
                isTouchBottom = false;
                break;
            case "Right":
                isTouchRight = false;
                break;
            case "Left":
                isTouchLeft = false;
                break;
        }
    }
}

 

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

public class Bullet : MonoBehaviour
{
    public int dmg;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BorderBullet")
        Destroy(gameObject); //모노비헤이어에 들어 있는 자체 함수 => 매개변수 오브젝트를 삭제하는 함수
    }
}

 

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public string enemyName;
    public float speed;
    public int health;
    public Sprite[] sprites;
    public float maxShotDelay; //실제 딜레이
    public float curShorDelay; //한발 쏜 후 딜레이
    public GameObject bulletObjA;
    public GameObject bulletObjB;
    public GameObject player;
    SpriteRenderer spriteRenderer;
    Rigidbody2D rigid;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        rigid = GetComponent<Rigidbody2D>();
        rigid.velocity = Vector2.down * speed;
    }
    private void Update()
    {
        Fire();
        Reload();
    }
    void Fire()
    {
        if (curShorDelay < maxShotDelay)
            return;

        if (enemyName == "S")
        {
            GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
            Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
            Vector3 dirVec = player.transform.position - transform.position;
            rigid.AddForce(dirVec.normalized * 3, ForceMode2D.Impulse);  // 방향 벡터를 정규화하여 힘을 가하는 부분 수정
        }
        else if (enemyName == "L")
        {
            GameObject bulletR = Instantiate(bulletObjB, transform.position + Vector3.right * 0.3f, transform.rotation);
            GameObject bulletL = Instantiate(bulletObjB, transform.position + Vector3.left * 0.3f, transform.rotation);
            Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
            Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
            Vector3 dirVecR = player.transform.position - (transform.position + Vector3.right * 0.3f);
            Vector3 dirVecL = player.transform.position - (transform.position + Vector3.left * 0.3f);
            rigidR.AddForce(dirVecR.normalized * 4, ForceMode2D.Impulse);  // 방향 벡터를 정규화하여 힘을 가하는 부분 수정
            rigidL.AddForce(dirVecL.normalized * 4, ForceMode2D.Impulse);  // 방향 벡터를 정규화하여 힘을 가하는 부분 수정
        }

        curShorDelay = 0;  // 발사 후 딜레이 초기화는 발사 이후에 처리되도록 이동
    }

    void Reload()
    {
        curShorDelay += Time.deltaTime;

    }
    private void OnHit(int dmg)
    {
        health -= dmg;
        spriteRenderer.sprite = sprites[1]; //피격시 1번
        Invoke("ReturnSprite", 0.1f);
        if(health <= 0)
        {
            Destroy(gameObject);
        }
    }

    void ReturnSprite()
    {
        spriteRenderer.sprite = sprites[0];
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "BorderBullet")
        {
            Destroy(gameObject );
        }
        else if(collision.gameObject.tag == "PlayerBullet")
        {
            Bullet bullet = collision.gameObject.GetComponent<Bullet>();
            OnHit(bullet.dmg);

            Destroy(collision.gameObject);
        }
    }

}

 

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

public class GameManager : MonoBehaviour
{
    public GameObject[] enemyObjs;
    public Transform[] spawnPoints;

    public float maxSpawnDelay;
    public float curSpawnDelay;

    public GameObject player;

    private void Update()
    {
        curSpawnDelay += Time.deltaTime;

        if(curSpawnDelay > maxSpawnDelay)
        {
            SpawnEnemy();
            maxSpawnDelay = Random.Range(0.5f, 3f);
            curSpawnDelay = 0;
        }
    }
    void SpawnEnemy()
    {
        int ranEnemy = Random.Range(0, 3); //소환될 적
        int ranPoint = Random.Range(0, 9); //소환될 위치
        GameObject enemy = Instantiate(enemyObjs[ranEnemy],
                                        spawnPoints[ranPoint].position,
                                        spawnPoints[ranPoint].rotation);
        Rigidbody2D rigid = enemy.GetComponent<Rigidbody2D>();
        //스피드도 가지고 와야한다.
        Enemy enemyLogic = enemy.GetComponent<Enemy>();
        //적비행기 생성 직후 플레이에 변수를 넘겨준다.
        enemyLogic.player = player;
        if (ranPoint == 5 || ranPoint == 6)
        {
            enemy.transform.Rotate(Vector3.back * 90); //z축으로 90도
            rigid.velocity = new Vector2(enemyLogic.speed * (-1), -1);
        }else if(ranPoint == 7 || ranPoint == 8)
        {
            enemy.transform.Rotate(Vector3.forward * 90); //z축으로 90도
            rigid.velocity = new Vector2(enemyLogic.speed, -1);
        }
        else
        {
            rigid.velocity = new Vector2(0, enemyLogic.speed * (-1));
        }
    }
    public void RespawnPlayer()
    {
        Invoke("RespawnPlayerExe", 2f);
        
    }
    private void RespawnPlayerExe()
    {
        player.transform.position = Vector3.down * 3.5f;
        player.SetActive(true);
    }
}

'산대특' 카테고리의 다른 글

2D Airplane - 원근감있는 무한 배경 만들  (0) 2024.03.17
2D Airplane - 5  (1) 2024.03.14
2D Airplane - 3  (0) 2024.03.12
2D Airplane - 2  (0) 2024.03.10
2D Airplane - 1  (0) 2024.03.09

녹화_2024_03_12_00_12_33_796.mp4
0.91MB

 

 

 

적들은 피해를 입어야하니 콜라이더  를 부착해야 되는데

이런 삼각형 물체는 박스콜라이더가 아닌 폴리건 콜라이더로 크기 조절을 할 수 있다.

 

그리고 이제 움직이는 속도가 있을 것이니 리지드바디 2D + 중력 0

 

그 후 태그 추가후 부착

적들이 필요한 요소추가와 지정한 속도로 내려올수 있도록 초기화

스프라이트 렌더러는 피격시 색이 흐려지므로 2개의 텍스쳐를 받기위하여 선언

피해를 받았을떄 체력은 데미지로 비례해서 감소하고

피격시 스프라이트 1번을 출력

그리고 0.1초후에 평상시 스프라이트인 0번을 출력할 것이고

체력이 0이하면 사라지고

또한 내려가다 벽과 부딫히거나 총알에 맞으면 파괴시킨다.

그런데 체력이 0이어야 사라져야 하므로 총알을 맞았을 경우와 그 데미지를 정해야한다.

 

각 총알에 파워 입력

적에게 체력과 스피드 부여

]

스프라이트도 각각 2개씩 넣어준다.

 

 

부딫히면 튕겨가는 것을 방지하기 위해 isTrigger체크

총알이 적과 부딫히면 관통하지 않고 사라지게 코드를 추가하였다.

피격시 스프라이트 변경과 Invoke함수를 통해 시간설정

 

만들어진 적들은 프리팹화 후 삭제

 

여기서 중요한건 프리팹화 할때 기존에 있던 자리가 저장되므로 모두 0 0 0 으로 초기화

게임을 관리할 게임매니저 생성

적을 정해진 시간동안 리스폰 할것이고

적의 수와 위치를 각각 지정

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

public class GameManager : MonoBehaviour
{
    public GameObject[] enemyObjs;
    public Transform[] spawnPoints;

    public float maxSpawnDelay;
    public float curSpawnDelay;

    private void Update()
    {
        curSpawnDelay += Time.deltaTime;

        if(curSpawnDelay > maxSpawnDelay)
        {
            SpawnEnemy();
            maxSpawnDelay = Random.Range(0.5f, 3f);
            curSpawnDelay = 0;
        }
    }
    void SpawnEnemy()
    {
        int ranEnemy = Random.Range(0, 3); //소환될 적
        int ranPoint = Random.Range(0, 5); //소환될 위치
        Instantiate(enemyObjs[ranEnemy],
            spawnPoints[ranPoint].position,
            spawnPoints[ranPoint].rotation);
    }
}

 

적들의 비행선이 보더에 걸리면사라지므로 씬보다 위 보더보다 아래에 에너미포인트 그룹을 위치시킨다.

각각의 포인트의 x좌표를 -1.8부터 1.8까지 위치

각각을 어싸인

 

using System.Collections;
using System.Collections.Generic;
using System.Net.Http.Headers;
using UnityEngine;

public class Player : MonoBehaviour
{
    public bool isTouchTop;
    public bool isTouchBottom;
    public bool isTouchRight;
    public bool isTouchLeft;
    private Animator anim;

    public float speed;
    public float power;
    public float maxShotDelay; //실제 딜레이
    public float curShorDelay; //한발 쏜 후 딜레이

    public GameObject bulletObjA;
    public GameObject bulletObjB;

    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    void Move()
    {
        float h = Input.GetAxisRaw("Horizontal");
        if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
            h = 0;
        float v = Input.GetAxisRaw("Vertical");
        if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
            v = 0;
        Vector3 curPos = this.transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
        transform.position = curPos + nextPos;

        if (Input.GetButtonDown("Horizontal") ||
            Input.GetButtonDown("Vertical"))
        {
            anim.SetInteger("Input", (int)h);
        }
    }

    private void Update()
    {
        Move();
        Fire();
        Reload();
    }

    void Fire()
    {
        if (!Input.GetButton("Fire1"))
            return;
        if (curShorDelay < maxShotDelay)
            return;

        switch (power)
        {

            case 1://Power One
                GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
                Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
                rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                break;
            case 2:
                GameObject bulletR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.1f, transform.rotation);
                GameObject bulletL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.1f, transform.rotation);
                Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
                rigidR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
            case 3:
                GameObject bulletRR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.3f, transform.rotation);
                GameObject bulletCC = Instantiate(bulletObjB, transform.position, transform.rotation);
                GameObject bulletLL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.3f, transform.rotation);
                Rigidbody2D rigidRR = bulletRR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidCC = bulletCC.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidLL = bulletLL.GetComponent<Rigidbody2D>();
                rigidRR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidCC.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidLL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
        }
                curShorDelay = 0;
    }

    void Reload()
    {
        curShorDelay += Time.deltaTime;

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Border")
        {
            switch(collision.gameObject.name)
            {
                case "Top":
                    isTouchTop = true;
                    break;
                case "Bottom":
                    isTouchBottom = true;
                    break;
                case "Right":
                    isTouchRight = true;
                    break;
                case "Left":
                    isTouchLeft = true;
                    break;
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        switch (collision.gameObject.name)
        {
            case "Top":
                isTouchTop = false;
                break;
            case "Bottom":
                isTouchBottom = false;
                break;
            case "Right":
                isTouchRight = false;
                break;
            case "Left":
                isTouchLeft = false;
                break;
        }
    }
}

 

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

public class Bullet : MonoBehaviour
{
    public int dmg;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BorderBullet")
        Destroy(gameObject); //모노비헤이어에 들어 있는 자체 함수 => 매개변수 오브젝트를 삭제하는 함수
    }
}

 

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed;
    public int health;
    public Sprite[] sprites;

    SpriteRenderer spriteRenderer;
    Rigidbody2D rigid;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        rigid = GetComponent<Rigidbody2D>();
        rigid.velocity = Vector2.down * speed;
    }

    private void OnHit(int dmg)
    {
        health -= dmg;
        spriteRenderer.sprite = sprites[1]; //피격시 1번
        Invoke("ReturnSprite", 0.1f);
        if(health <= 0)
        {
            Destroy(gameObject);
        }
    }

    void ReturnSprite()
    {
        spriteRenderer.sprite = sprites[0];
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "BorderBullet")
        {
            Destroy(gameObject );
        }
        else if(collision.gameObject.tag == "PlayerBullet")
        {
            Bullet bullet = collision.gameObject.GetComponent<Bullet>();
            OnHit(bullet.dmg);

            Destroy(collision.gameObject);
        }
    }

}

 

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

public class GameManager : MonoBehaviour
{
    public GameObject[] enemyObjs;
    public Transform[] spawnPoints;

    public float maxSpawnDelay;
    public float curSpawnDelay;

    private void Update()
    {
        curSpawnDelay += Time.deltaTime;

        if(curSpawnDelay > maxSpawnDelay)
        {
            SpawnEnemy();
            maxSpawnDelay = Random.Range(0.5f, 3f);
            curSpawnDelay = 0;
        }
    }
    void SpawnEnemy()
    {
        int ranEnemy = Random.Range(0, 3); //소환될 적
        int ranPoint = Random.Range(0, 5); //소환될 위치
        Instantiate(enemyObjs[ranEnemy],
            spawnPoints[ranPoint].position,
            spawnPoints[ranPoint].rotation);
    }
}

 

'산대특' 카테고리의 다른 글

2D Airplane - 원근감있는 무한 배경 만들  (0) 2024.03.17
2D Airplane - 5  (1) 2024.03.14
2D Airplane - 4  (0) 2024.03.12
2D Airplane - 2  (0) 2024.03.10
2D Airplane - 1  (0) 2024.03.09

총알도 똑같이 잘라준다.

 

총알 충돌 시 이벤트가 생기므

총알에 박스콜라이더와 리지드바디 추가

에드포스로 날릴것이므로 리지드바디 -> 다이나믹

중력은 받지 않으므로 - 그래비티 스케일 0

재사용해서 사용할것이므로 프리팹화

총알 A를 복사해서 만든후 스프라이트에 다른 모양을 넣은 후 콜라이더 크기 조절

 

총알B를 저장하면 A를 복사해서 만든 것이므로 다시 프리팹화할때 알림 문구가 뜨는데

새로운 것이면 Original클릭

 

총알이 나중에 무수히 많아질 수 있으므로 지울 수 있는 또 다른 경계선 생성할 것인데

Bullet이라는 스크립트를 새로 생성

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

public class Bullet : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BorderBullet")
        Destroy(gameObject); //모노비헤이어에 들어 있는 자체 함수 => 매개변수 오브젝트를 삭제하는 함수
    }
}

기존에 만들어 놓았던 Border를 복사해서 이름을 바꾸고

경계선을 재설정

 

태그를 새로 만든 후 할당

 

그럼 총알이 경계선에 닿으면 사라진다.

 

이제 총알을 만들 로직을 Player 스크립트에 작성할 것이다.

전 페이지에서 만들었던 업데이트문안에 있는 함수를 따로 캡슐화로 분리한 후 이름을 Move로 바꾸었고,

총알을 만들며 총알이 충돌을 일으켜야하므로 리지드바디를 사용

 

https://docs.unity3d.com/ScriptReference/ForceMode2D.html

 

Unity - Scripting API: ForceMode2D

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

퍼블릭으로 만들어둔 공간에 프리팹 할당

실행해보면 무수히 만들어진다.

 

그림과 같이 총알끼리 물리적 충돌로 인해 여러뱡항으로 나오는데

이것은 프리팹에서 isTrigger를 체크하면 해결된다.

 

총알이 눌렀을 때만 발사되게 조건을 넣어줄 것인데

!를 사용하는것이 아닌 조건일때만 실행되게 코드를 변경해줘도 된다.(코드 스타일)

이제 다음은 총알 딜레이를 추가할 것이다.

시간에 따라 현재 딜레이에 추가해줄것이고 

발사는 딜레이에 영향을 받아줄 것이므로 Fire문 안에 넣는다.

그리고 발사 후 딜레이를 없애주기 위해 마지막엔 딜레이를 0으로 초기화

총알을 쏠 때마다 딜레이를 변경해줄 수 있다.

 

총알의 파워를 입력해줄 것인데

케이스로 나눠서 각각 파워1, 2, 3일때 다르게 나오게 수치 변경

그리고 총알마다 간격을 조절하여

현재 위치해서 벡터값을 더해주면

총알의 생성위치를 원하는 간격으로 조절할 수 있다.

 

완성된 코드

using System.Collections;
using System.Collections.Generic;
using System.Net.Http.Headers;
using UnityEngine;

public class Player : MonoBehaviour
{
    public bool isTouchTop;
    public bool isTouchBottom;
    public bool isTouchRight;
    public bool isTouchLeft;
    private Animator anim;

    public float speed;
    public float power;
    public float maxShotDelay; //실제 딜레이
    public float curShorDelay; //한발 쏜 후 딜레이

    public GameObject bulletObjA;
    public GameObject bulletObjB;

    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    void Move()
    {
        float h = Input.GetAxisRaw("Horizontal");
        if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
            h = 0;
        float v = Input.GetAxisRaw("Vertical");
        if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
            v = 0;
        Vector3 curPos = this.transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
        transform.position = curPos + nextPos;

        if (Input.GetButtonDown("Horizontal") ||
            Input.GetButtonDown("Vertical"))
        {
            anim.SetInteger("Input", (int)h);
        }
    }

    private void Update()
    {
        Move();
        Fire();
        Reload();
    }

    void Fire()
    {
        if (!Input.GetButton("Fire1"))
            return;
        if (curShorDelay < maxShotDelay)
            return;

        switch (power)
        {

            case 1://Power One
                GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
                Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
                rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                break;
            case 2:
                GameObject bulletR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.1f, transform.rotation);
                GameObject bulletL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.1f, transform.rotation);
                Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
                rigidR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
            case 3:
                GameObject bulletRR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.3f, transform.rotation);
                GameObject bulletCC = Instantiate(bulletObjB, transform.position, transform.rotation);
                GameObject bulletLL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.3f, transform.rotation);
                Rigidbody2D rigidRR = bulletRR.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidCC = bulletCC.GetComponent<Rigidbody2D>();
                Rigidbody2D rigidLL = bulletLL.GetComponent<Rigidbody2D>();
                rigidRR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidCC.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                rigidLL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

                break;
        }
                curShorDelay = 0;
    }

    void Reload()
    {
        curShorDelay += Time.deltaTime;

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Border")
        {
            switch(collision.gameObject.name)
            {
                case "Top":
                    isTouchTop = true;
                    break;
                case "Bottom":
                    isTouchBottom = true;
                    break;
                case "Right":
                    isTouchRight = true;
                    break;
                case "Left":
                    isTouchLeft = true;
                    break;
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        switch (collision.gameObject.name)
        {
            case "Top":
                isTouchTop = false;
                break;
            case "Bottom":
                isTouchBottom = false;
                break;
            case "Right":
                isTouchRight = false;
                break;
            case "Left":
                isTouchLeft = false;
                break;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BorderBullet")
        Destroy(gameObject); //모노비헤이어에 들어 있는 자체 함수 => 매개변수 오브젝트를 삭제하는 함수
    }
}

 

'산대특' 카테고리의 다른 글

2D Airplane - 원근감있는 무한 배경 만들  (0) 2024.03.17
2D Airplane - 5  (1) 2024.03.14
2D Airplane - 4  (0) 2024.03.12
2D Airplane - 3  (0) 2024.03.12
2D Airplane - 1  (0) 2024.03.09

 

 

 

 

스프라이트 게임을 자르기 위해 설정들을 이렇게 바꾸어 주었다.

그 후 Sprite Editor를 누르면 여러개의 이미지들이 묶여있는 것을 분할 할 수 있는데

크기로 24*24 여백을 1로 잘랐다.

 

우선 화면의 비율을 선택해주자

필자는 aspect로 하였고 비율을 입력하여 2가지 해상도를 만들었다.

 

비행기가 움직일 수 있게 코드 작성

transform은 time.deltatime이랑 세트로 생각하자!

현재의 위치에 키보드로 받을 위치를 더해준다.

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

public class Player : MonoBehaviour
{
    public float speed;
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        Vector3 curPos = this.transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
        transform.position = curPos + nextPos;

    }
}

 

움직여보면 원하지 않는 배경 밖으로 나가게 된다.

 

비행기에 Box Collider를 추가하여 부착해주면 초록색 경계가 생긴다.

 

피격 범위로도 사용할 것이므로 콜라이더의 크기를 줄여주었다.

맵의 바깥 부분에 Border라는 빈 오브젝트안에

방향 별로 box collider2D를 붙인 후 범위를 잡아주었다.

 

 

Player :

물리 연산이기에 모두 리지드바디2d적용

물리엔진을 많이 사용하지 않기에 키네틱을 사용

키넥틱이란? 다른 것이랑 충돌이 일어났을 때 영향을 받지 않겠다.

 

보더들은 고정이기때문에 static 사용

 

경계들을 각각 인지를 해야하기에 변수 선언

물리적인 힘은 넣을 필요 없으므로 collider대신에 온트리거 사용하였고,

각 경계마다 tag로 Border를 달아줄 것이다.

물체가 닿은 상태에서 h는 좌우 이동이므로 => 이동시 닿을 떄를 처리하는데 이때 각각 0으로 만들어서 이동을 멈춘다.

물체가 닿지 않았을 때 이동해야하므로 그 경우도 처리해야 한다.

태그를 달아준다.

 

현재 까지의 코드

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

public class Player : MonoBehaviour
{
    public float speed;
    public bool isTouchTop;
    public bool isTouchBottom;
    public bool isTouchRight;
    public bool isTouchLeft;

    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
            h = 0;
        float v = Input.GetAxisRaw("Vertical");
        if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
            v = 0;
        Vector3 curPos = this.transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
        transform.position = curPos + nextPos;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Border")
        {
            switch(collision.gameObject.name)
            {
                case "Top":
                    isTouchTop = true;
                    break;
                case "Bottom":
                    isTouchBottom = true;
                    break;
                case "Right":
                    isTouchRight = true;
                    break;
                case "Left":
                    isTouchLeft = true;
                    break;
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        switch (collision.gameObject.name)
        {
            case "Top":
                isTouchTop = false;
                break;
            case "Bottom":
                isTouchBottom = false;
                break;
            case "Right":
                isTouchRight = false;
                break;
            case "Left":
                isTouchLeft = false;
                break;
        }
    }
}

 

 

이제 애니메이션을 넣어줄 것이다.

잘라놓은 애니메이션 각각 4개를 클릭해서

하이어라이키에 있는 Player에 드래그해서 넣어주고 애니메이션 폴더를 생성후

각각 Center, Left, Right로 이름을 만들어서 저

플레이어에 애니메이션 컴포넌트가 생성됨

그 후 플레이어(애니메이션 컨트롤러)를 눌러서 이 창을 띄어주고

Make Transition으로 서로의 관계를 만들어 준다.

 

트랜지션은 파라미터로 이동한다.

매개변수 Input.GetAxisRaw() 값 그대로 사

각각의 트랜지션 설정을 해줄것인다.

즉각 반응을 위해 Has Exit Time은 꺼야한다.

Transition Duration은 보통3D에서 모션을 부드럽게 변화시키기 위해 사용하는데

필요 없으므로 0으로 변경

 

이제 인풋 파라미터를 받을 코드를 작성할 것이다.

그전에 애니메이터 변수 생성 후 초기화 진행

 

업데이트 문 안에 작성하고 좌우 방향은 h이므로 h를 넣고 float형이므로

(int)로 강제 형변환

\

배경을 집어 넣을 것인데 넣으면 플레이어가 보이지 않을 수 있다.

그러면 플레이어를 누르고 Order In Layer를 0에서 1로 변경

 

그러면 플레이어가 이제 잘보일 것이다.

마지막을 플레이를 해볼것인데 애니메이터 컨트롤러를 클릭하고 실행하면

실시간 애니메이션이 변경되며 사용됨을 눈으로 확인 할 수 있다.

애니메이터 실시간.mp4
0.71MB

'산대특' 카테고리의 다른 글

2D Airplane - 원근감있는 무한 배경 만들  (0) 2024.03.17
2D Airplane - 5  (1) 2024.03.14
2D Airplane - 4  (0) 2024.03.12
2D Airplane - 3  (0) 2024.03.12
2D Airplane - 2  (0) 2024.03.10

 

 

 

 

 

https://docs.unity3d.com/ScriptReference/Animator.SetBool.html

 

Unity - Scripting API: Animator.SetBool

Use Animator.SetBool to pass Boolean values to an Animator Controller via script. Use this to trigger transitions between Animator states. For example, triggering a death animation by setting an “alive” boolean to false. See documentation on Animation

docs.unity3d.com

 

테스트를 해보려는 도중 점프가 되지 않아서 원인을 찾아보니 Simulated를 체크하지 않았다.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class TokoController : MonoBehaviour
{
    public AudioClip deathClip;
    public float jumpForce = 700f;
    private int jumpCount = 0;
    private bool isGrounded = false;
    private bool isDead = false;

    private Rigidbody2D playerRigidbody;
    private Animator animator;
    private AudioSource playerAudio;

    void Start()
    {
        //게임 오브젝트로부터 사용할 컴포넌트들을 가져와 변수에 할당
        playerRigidbody = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        playerAudio = GetComponent<AudioSource>();
    }

    void Update()
    {
        if (isDead)
        {
            //사망시 더 이상 처리를 하지 않고 종료
            return;
        }
        if (Input.GetMouseButtonDown(0) && jumpCount < 2)
        {
            jumpCount++; //점프가 두번됨
            //점프 직전 속도를 순간적으로 제로(0,0)으로 변경
            playerRigidbody.velocity = Vector2.zero;
            //리지드바디에 위쪽으로 힘 주기
            playerRigidbody.AddForce(new Vector2(0, jumpForce));
            //오디오 소스 재생
            playerAudio.Play();
            Debug.Log("마우스가 클릭됨");

        }
        else if(Input.GetMouseButtonUp(0) && playerRigidbody.velocity.y > 0) // 마우스에서 손을 떼는 순간 속도의 y값이 양수라면(위로 상승) => 현재 속도를 절반으로 변경
        {
            playerRigidbody.velocity = playerRigidbody.velocity * 0.5f;
        }
        //애니메이터의 Grounded 파라미터를 isGrounded 값으로 갱신
        animator.SetBool("Grounded", isGrounded);
        Debug.LogFormat("{0}", playerRigidbody.velocity.y);

    }

    private void Die()
    {

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        //트리거 콜라이더를 가진 물체와 충돌 감지
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        //바닥에 닿았음을 감지

        //어떤 콜라이더와 닿았으며, 충돌표면이 위쪽을 보고 있으면
        if (collision.contacts[0].normal.y > 0.7f)
        {
            //isGrouded가 true로 변경하고, 누적 점프횟수를 0으로 초기화
            isGrounded = true;
            jumpCount = 0;
            //Debug.Log("충돌 발생");
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        //바닥에서 벗어났음을 감지하는 처리
        //어떤 콜라이더에서 떼어진 경우
        isGrounded = false;
    }


}

 

velocity.y의 값을 출력해 보았는데 음수가 나왔다.

Rigidbody의 velocity.y 값이 음수가 나오는 이유는 중력의 영향 때문이다.

Rigidbody의 velocity는 해당 객체의 현재 속도를 나타내며, 유니티에서는 아래쪽 방향을 음의 방향으로 간주한다..

따라서 캐릭터가 점프 후에 일정 높이에 도달하면 중력에 의해 아래로 가속됩니다. 이 때 velocity.y 값은 음수가 된다.

그리고 바닥에 닿으면 다시 상승하기 시작할 때 velocity.y 값은 양수가 될 것이다.

즉, velocity.y 값이 음수가 되는 것은 캐릭터가 점프 후 중력에 의해 아래로 가속되는 정상적인 동작이다.

 

 

 

private void Die()
{
    animator.SetTrigger("Die");
    playerAudio.clip = deathClip;
    playerAudio.Play();

    //속도를 제로로 변경
    playerRigidbody.velocity = Vector2.zero;
    //사망 상태를 true로 변경
    isDead = true;
}

다른 Set계열 메서드와 달리 파라미터에 할당할 새로운 값은 입력하지 않는다.

SetTrigger()메서드는 '방아쇠''를 당길 뿐이다.

트리거 타입의 파라미터는 즉시 true가 되었다가 곧바로 false가 되기 때문에 별도의 값을 지정하지 않는다.

animator.SetTrigger("Die")가 실행되면 곧바로 Die상태러 전환되는 애니메이션 클립이 재생된다.

 

배경을 설정할 것인데 태그에다 이름을 지정하면 order in layer 처럼

레이어의 순서가 정해진다.

 

배경을 스크롤링 할것이므로 스크립트 추가

 

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

public class ScrollingObject : MonoBehaviour
{
    public float speed = 10f;
   

    void Update()
    {
        this.transform.Translate(Vector3.left * speed  * Time.deltaTime);
    }
}

배경과 발판에 스크립트를 부착해서 왼쪽으로 10f 속도로 이동하게 하였기 때문에

플레이어인 Toko는 지나가다 발판이 없어서 떨어진다.

 

Sky에 박스 콜라이더를 넣어주고 isTrigger를 체크하지 않으면

발판과 동시에 

인식을 제대로 하지 못하므로 isTrigger을 체크해야한다.

 

https://docs.unity3d.com/kr/2021.3/Manual/CollidersOverview.html

 

콜라이더 - Unity 매뉴얼

Collider 컴포넌트는 물리적 충돌을 위해 게임 오브젝트의 모양을 정의합니다. 보이지 않는 콜라이더는 게임 오브젝트의 메시와 완전히 똑같을 필요는 없습니다. 메시의 대략적인 근사치로도 효

docs.unity3d.com

 

배경을 무한 반복을 스크립트 추가

 

offset은 Vector2타입지만 transform.position은 Vector3타입이다.

그래서 (Vector2)로 형변환하여 사용

Vector2 값을 Vector3변수에 할당하는 것은 가능한데,,,

이 경우 z값이 0인 Vector3로 자동 형변환되어 할당된다.

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

public class BackgroudLoop : MonoBehaviour
{
    private float width; //배경의 가로 길이
    private void Awake()
    {
       BoxCollider2D col = GetComponent<BoxCollider2D>();
        this.width = col.size.x;
        Debug.Log(this.width);
    }

    void Update()
    {
        if(transform.position.x <= -this.width)
        {
            this.Reposition();
        }
    }

    void Reposition()
    {
        //현재 위치에서 오른쪽으로 가로 길이 * 2 만큼 이동
        Vector2 offset = new Vector2(this.width * 2f, 0);
        transform.position = (Vector2)this.transform.position + offset;
    }


}

sky를 복사해서 기존에 있는 스크린에 넣는다면

예를 들어 화면이 sky -> sky(1) -> sky -> sky(1)이런식으로 두배 간격으로 복사된다.

 

이제 발판을 더 만들 것인데 프리팹으로 만들었고

프리팹을 가져와서 생성된 게임오브젝트에 설정을 바꾸었다.

 

만들어진 발판 프리팹에 Obstacles를 추가할건데 이것을 추가할 때는

프리팹에서 만드는 것이 좋다.

 

그후 발판은 fore 방해물은 middle로 설정하였다.

 

발판 스크립트를 만들었고 방해물 3개가 각각 랜덤으로 나올수 있게 설정

그리고 방해물 회피 성공시 +1

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

public class Platform : MonoBehaviour
{
    public GameObject[] obstacles;
    private bool stepped = false;

    private void OnEnable()
    {
        stepped = false;
        for (int i = 0; i < obstacles.Length; i++)
        {
            if (Random.Range(0, 3) == 0)
            {
                obstacles[i].SetActive(true);
            }
            else
            {
                obstacles[i].SetActive(false);
            }
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Player") && !stepped)
        {
            stepped = true;
            Debug.Log("점수 1점 추가");
        }
    }
}

 

그후 아까 했던 플랫폼의 모든 프리팹에 적용 되도록 Apply All

 

UI를 만들진 않았지만 UI라던지 게임을 관리할 게임 매니저를 만들었다.

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

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public bool isGameover = false; //게임오버 상태
    public GameObject gameoverUI; //게임 오버 시 활성화 할 UI 게임 오브젝트 

    private int score = 0; //게임점수
    void Awake()
    {//싱글턴 변수 instance가 비어있는가?
        if (instance == null)
        {
            //instance가 비어있다면 그곳에 자기 자신을 할당
            instance = this;
        }
        else
        {
            Destroy(this.gameObject);
        }
    }
    private void Update()
    {
        if (isGameover && Input.GetMouseButtonDown(0))
        {
            //게임오버 상태에서 마우스 왼쪽 버튼 클릭하면 현재 씬 재시작
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            //현재 활성화된 씬의 정보를Scene 타입의 오브젝트로 가져오는 메서드 씬의 이름을 변수 name으로 제공
        }
    }
    public void AddScore(int newScore)
    {
        //게임오버가 아니라면
        if (!isGameover)
        {
            //점수를 증가
            score += newScore;
            //scoreTect.text = "Score : " + score;
        }
    }
    public void OnPlayerDead()
    {
        isGameover = true;
        //  gameoverUI.SetActive(true);
    }

}

 

이제 platform이 랜덤으로 생성될 것이니 하이어라이키에 있는 플랫폼프리팹을 지워준후

많아질 데이터를 관리하기 위해 오브젝트 풀링사용하여

플랫폼 스폰서를 만들어서 복제된 sky(1)에 할당

오브젝트 풀링
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformSpawner : MonoBehaviour
{
    public GameObject platformPrefab;
    public int count = 3;
    private GameObject[] platforms;

    private float lastSpawnTime = 0;
    private float timeBetSpawn = 0;

    private int currentIndex = 0;

    private void Start()
    {
        platforms = new GameObject[count];

        //미리만들자 
        for (int i = 0; i < count; i++)
        {
            platforms[i] = Instantiate(platformPrefab, new Vector3(0, -25, 0), Quaternion.identity);
        }
    }

    private void Update()
    {
        if (Time.time >= lastSpawnTime + timeBetSpawn)
        {
            lastSpawnTime = Time.time;
            timeBetSpawn = Random.Range(1.25f, 2.25f);
            float yPos = Random.Range(-3.5f, 1.5f);

            //OnEnable호출을 위해 
            this.platforms[currentIndex].SetActive(false);
            this.platforms[currentIndex].SetActive(true);

            //재배치 
            this.platforms[currentIndex].transform.position = new Vector2(20, yPos);

            //인덱스 증가 
            currentIndex++;

            //마지막 순번에 도달 했다면 순번 리셋 
            if (currentIndex >= count)
            {
                currentIndex = 0;   //0, 1, 2, 0, 1, 2, 0, 1, 2....
            }
        }
    }
}



오브젝트 풀링 사용하는 이유

유니티에서 오브젝트를 생성하기 위해서는 Instantiate를 사용하고 삭제할 때는 Destroy를 사용

하지만 Instantiate, Destroy 이 두 함수는 무게가 상당히 크다.

Instantiate(오브젝트 생성)은 메모리를 새로 할당하고 리소스를 로드하는 등의 초기화 과정이 필요하고,
Destroy(오브젝트 파괴)는 파괴 이후에 발생하는 가비지 컬렉팅으로 인한 프레임 드랍이 발생할 수 있다.


쉽게 말해 "재사용" 이라고 볼 수 있다.


메모리를 할당 해두기 때문 메모리를 희생하여 성능을 높이는 것이지만,
실시간 작동하는 게임에서 프레임 때문에 선호된다.

--- Inspector ---

 

녹화_2024_03_05_22_52_50_643.mp4
2.89MB

'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글

Zombie - Line Render  (0) 2024.03.07
Tank - Rigidbody.MovePosition  (0) 2024.03.06
Quaternion  (1) 2024.03.04
Dodge game  (0) 2024.03.04
코루틴 연습  (0) 2024.03.03

 

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

public class Player : MonoBehaviour
{
    public float speed = 1f;
    public Rigidbody playerRigidbody;
    public GameObject bulletPrefab;

    void Update()
    {
        PlayerMove();
    }

    private void PlayerMove()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(h, 0, v) * speed;
        transform.Translate(movement * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        // 충돌한 오브젝트가 총알인지 확인
        if (other.CompareTag("Bullet"))
        {
            // 플레이어를 파괴
            Destroy(gameObject);
            Debug.Log("Game over");
        }
    }

}

 

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

public class Enemy : MonoBehaviour
{
    [SerializeField] GameObject player;
    [SerializeField] public GameObject bulletPrefab;
    private float spawnInterval = 3f; // 총알 생성 간격
    private Vector3 spawnPosition; // 총알이 생성될 위치
    void Start()
    {
        spawnPosition = this.gameObject.transform.position; // 총알이 생성될 위치 초기화
        StartCoroutine(SpawnBullet()); // 코루틴 시작
    }

    IEnumerator SpawnBullet()
    {
        while (player != null)
        {
            // 총알 생성
            Instantiate(bulletPrefab, spawnPosition, Quaternion.identity);

            // 일정한 간격 기다리기
            yield return new WaitForSeconds(spawnInterval);
        }
    }
}

 

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

public class Bullet : MonoBehaviour
{
    public float speed = 2f;
    public GameObject player;
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
            // Rigidbody 컴포넌트 가져오기
            Rigidbody bullets = GetComponent<Rigidbody>();
            Vector3 direction = (player.transform.position - transform.position).normalized;
            transform.forward = direction;
            // 총알의 이동 방향 설정 및 속도 적용
            bullets.velocity = transform.forward * speed;

            // 5초 후에 총알 파괴
            Destroy(gameObject, 4.5f);
    }
    private void OnTriggerEnter(Collider other)
    {
        // 충돌한 오브젝트가 총알인지 확인
        if (other.CompareTag("Player"))
        {
            Destroy(gameObject);
        }
        if (other.CompareTag("Wall"))
        {
            Destroy(gameObject);
        }
    }

}

'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글

UniRun  (4) 2024.03.05
Quaternion  (1) 2024.03.04
코루틴 연습  (0) 2024.03.03
StarCraft - Tank  (0) 2024.02.29
StarCraft - DropShip Logic  (0) 2024.02.28

1. ground 요소를 추가하여  Rigidbody 2D -> Body Type - Kinematic 설정하여

땅을 만들어 공중에 떠있게 만들어 주었다.

2. 캔버스 레거시 text로 velocityText를 만들어서 고양이가 제자리부터 이동거리를 수치로 표시

    -  ClimbCloudGameDirector 스크립트를 추가하고 CatController에 연결해줌으로 써 이동거리 표시

3. 고양이 선택후 Tool bar에서 Window - Animation- Animation에 들어가서 Create를 누른후 

     Assets안에 애니메이션 폴더를 만들어주고 걷는 애니메이션을 넣을 것으므로 이름을 Walk로 생성

     Animation탭 안에서 add property를 누르고 sprite 선택 후 적적할게 고양이가 나눠져서 걷는 그림을 넣어주었다.

    마지막으로 애니메이션 탭 안 우측 상단에 있는 add keyframe을 누른 후 적당한 거리 뒤에 넣으면

    마지막 프레임이 복사되고 그 프레임 안까지의 사진들이 연속적으로 실행된다.

    애니메이터는 CatController와 같은 위치에 있으므로 따로 호출하지 않아도 클래스 안에서 사용가능

    애니메이션속도를 캐릭터 움직임 속도에 맞춰 변하도록 설정

    this.anim.speed = (Mathf.Abs(this.rbody.velocity.x) / 2f);

 

4. 물리엔진을 사용해서 충돌판정

1. 둘중 하나는 리지드바디 컴포넌트가 있어야 한다

2. 두 객체모두 콜라이더가 있어야 한다

3. isTrigger 모드를 체크 한다

 

 

트리거모드는 업데이트보다 먼저 일어난다

 

5. 깃발에 도달(충돌)했을 때 화면 전환

6. 그후 화면을 터치하면 다시 복귀

7. 고양이 이동가능 거리 조절

8. 고양이가 공중에서 계속 점프하지 못하게 조건문 추가

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Assertions.Comparers;
using UnityEngine.SceneManagement;

public class CatController : MonoBehaviour
{
    [SerializeField] private Rigidbody2D rbody;
    [SerializeField] private float moveForce = 100f;
    [SerializeField] private float jumpForce = 680f;

    [SerializeField]
    private ClimbCloudGameDirector gameDirector;

    private Animator anim;

    private bool hasSpace = false;

    private void Start()
    {
        //this.gameObject.GetComponent<Animation>();

        anim = GetComponent<Animator>();

        //this.gameDirector = GameObject.Find("GameDirector").GetComponent<ClimbCloudGameDirector>();
        //this.gameDirector = GameObject.FindAnyObjectByType<ClimbCloudGameDirector>();

    }

    void Update()
    {
        //스페이스바를 누르면 
        if (Mathf.Abs(rbody.velocity.y) < 0.01f)

        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (!hasSpace)
                {
                    //힘을 가한다 
                    this.rbody.AddForce(this.transform.up * this.jumpForce);
                    //this.rbody.AddForce(Vector3.up * this.force);

                        hasSpace = false; // 다시 점프할 수 있도록 허용

                }
            }


        }
        // -1, 0, 1 : 방향 
        int dirX = 0;
        //왼쪽화살표키를 누르고 있는 동안에 
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            dirX = -1;
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            dirX = 1;
        }

        // Debug.Log(dirX); //방향 -1, 0, 1

        //스케일 X를 변경 하는데 키가 눌렸을 때만 
        //키가 눌렸을때만 = (dirX != 0)
        if (dirX != 0)
        {
            this.transform.localScale = new Vector3(dirX, 1, 1);
        }


        //벡터의 곱 
        //Debug.Log(this.transform.right * dirX);  //벡터3

        //도전 ! : 속도를 제한하자 
        //velocity.x 가 3정도가 넘어가니깐 빨라지는거 같드라구...
        if (Mathf.Abs(this.rbody.velocity.x) < 3)
        {
            this.rbody.AddForce(this.transform.right * dirX * moveForce);
        }

        this.anim.speed = (Mathf.Abs(this.rbody.velocity.x) / 2f);
        this.gameDirector.UpdateVelocityText(this.rbody.velocity);


        // Debug.Log(this.transform.position);

        float clampX = Mathf.Clamp(this.transform.position.x, -2.39f, 2.35f);
        Vector3 pos = this.transform.position;
        pos.x = clampX;
        this.transform.position = pos;


    }

    // Trigger 모드 일경우 충돌 판정을 해주는 이벤트 함수
    private bool hasEntered = false;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!hasEntered)
        {
            Debug.LogFormat("OnTriggerEnter2D: {0}", collision);
            SceneManager.LoadScene("ClimbCloudClear");
            hasEntered = true; // 한 번 이벤트가 발생하면 이 변수를 true로 설정하여 두 번 이상 호출되지 않도록 함

        }

    }



}

 

using System.Collections;
using System.Collections.Generic;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ChangeScene : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            
            SceneManager.LoadScene("ClimbCloud");
            Debug.Log("화면이 전환됨");
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ClimbCloudGameDirector : MonoBehaviour
{
    [SerializeField] private Text velocityText;

    public void UpdateVelocityText(Vector2 velocity)
    {
        float velocityX = Mathf.Abs(velocity.x);
        this.velocityText.text = velocityX.ToString();
    }
}


+ 개선할 점

 

카메라가 고양이 따라가기

'산대특 > 게임 알고리즘' 카테고리의 다른 글

Pirate Bomb - Captain(Enemy)  (0) 2024.02.02
Pirate Bomb - BombGuy  (0) 2024.02.02
ClimbCloud  (2) 2024.02.01
C# 대리자, 람다함수  (1) 2024.01.31
Git, SourceTree  (1) 2024.01.31

+ Recent posts