Untitled

일반 탄환

private void Start() {
        rigid = GetComponent<Rigidbody2D>();
        Direction();
    }

    private void Update() {
        Move();
    }

    public void Move(){
        transform.Translate(Vector3.right * bulletSpeed * dir * Time.deltaTime);
    }
    public void Direction(){
        if(dir ==  -1){
            //spRender.flipX = true;
            gameObject.transform.localScale = new Vector2(-gameObject.transform.localScale.x, gameObject.transform.localScale.y);
        }
    }
    
    private void OnTriggerEnter2D(Collider2D other) {
        if(other.gameObject.tag == "groundMask"){         //탄막 제거
            Destroy(gameObject);       //게임 오브젝트 삭제
        }

        if(other.gameObject.tag == "Enemy"){                //적에게 닿을 시
            other.GetComponent<Enemy>().Damaged(bulletDamage);
            Destroy(gameObject);
        }
    }

일반탄환은 Instantate에 의해 생성되는 것을 기반으로 생성된 후 플레이어 공격 스크립트에서 변수(공격방향, 데미지)를 변경한다.

이때의 Direction 함수는 두개의 도형으로 탄환의 모양을 만들게 되어 두 개체의 위치 및 방향을 재조정하기위해 존재한다.

탄환은 지형 및 몬스터에게 닿을 시 파괴된다. 데미지 연산은 몬스터스크립트에서 이루어진다.

Untitled

private void Start() {
        rigid = GetComponent<Rigidbody2D>();
        boxCol = GetComponent<BoxCollider2D>();
        Invoke("EndAtkStart", chargingAtkTime);
        RigidBodyBlink();
        transform.localScale = new Vector2(transform.localScale.x, charging);
        bulletHeight = transform.localScale.y;
    }

    private void Update() {
        DestroyAnim();
    }   
    
    private void OnTriggerEnter2D(Collider2D other) {
        if(other.gameObject.tag == "Enemy"){                //적에게 닿을 시
            other.GetComponent<Enemy>().Damaged(bulletDamage);
        }
    }

    void RigidBodyBlink(){
        if(boxCol.enabled == true){
            boxCol.enabled = false;
        }else{
            boxCol.enabled = true;
        }
        Invoke("RigidBodyBlink", blinkTime);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////Destroy

    void EndAtkStart(){
        endAtk = true;
    }

    void DestroyAnim(){
        if(endAtk == true){
            transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y - 0.05f);
            if(transform.localScale.y <= 0){ 
                Destroy();
            }
        }
    }

    private void Destroy(){
        Destroy(gameObject);
    }

차징 공격은 차징 수준에 따라 공격의 폭, 지속시간, 데미지를 다르게 하기위해 각 변수는 플레이어 공격 스크립트에서 조정된다.

차징 게이지

차징 게이지

차징공격이 의도한 시간만큼 이루어 진 뒤 더이상의 물리연산을 제한하기 위해 콜라이더를 비활성화하고 공격이 끝날 때 갑자기 사라지는 연출보다 자연스럽게 하기 위해 크기를 y축을 기준하여 줄어들게 하였다.

Full Code

일반 탄환

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

public class Bullet : MonoBehaviour
{
    public GameManager gameManager;
    public float bulletDamage;
    public float dir;
    public float bulletSpeed;
    Rigidbody2D rigid;
    SpriteRenderer spRender;

    private void Start() {
        rigid = GetComponent<Rigidbody2D>();
        Direction();
    }

    private void Update() {
        Move();
    }

    public void Move(){
        transform.Translate(Vector3.right * bulletSpeed * dir * Time.deltaTime);
    }
    public void Direction(){
        if(dir ==  -1){
            //spRender.flipX = true;
            gameObject.transform.localScale = new Vector2(-gameObject.transform.localScale.x, gameObject.transform.localScale.y);
        }
    }
    
    private void OnTriggerEnter2D(Collider2D other) {
        if(other.gameObject.tag == "groundMask"){         //탄막 제거
            Destroy(gameObject);       //게임 오브젝트 삭제
        }

        if(other.gameObject.tag == "Enemy"){                //적에게 닿을 시
            other.GetComponent<Enemy>().Damaged(bulletDamage);
            Destroy(gameObject);
        }
    }
}

차징 탄환

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

public class Bullet_Charging : MonoBehaviour
{
    public GameManager gameManager;
    public float bulletDamage;  //데미지
    public float dir;           //방향
    public float bulletHeight;  //공격 크기(y축)
    public float charging;      //충전 정도 - 플레이어로부터 받는다
    public bool endAtk;         //공격 종료 애니메이션을 시작시키는 트리거
    public float chargingAtkTime;   //차징공격 지속 시간
    public float blinkTime;     //공격 연타 간격(시간)     
    
    Rigidbody2D rigid;
    SpriteRenderer spRender;
    BoxCollider2D boxCol;

    private void Start() {
        rigid = GetComponent<Rigidbody2D>();
        boxCol = GetComponent<BoxCollider2D>();
        Invoke("EndAtkStart", chargingAtkTime);
        RigidBodyBlink();
        transform.localScale = new Vector2(transform.localScale.x, charging);
        bulletHeight = transform.localScale.y;
    }

    private void Update() {
        DestroyAnim();
    }   
    
    private void OnTriggerEnter2D(Collider2D other) {
        if(other.gameObject.tag == "Enemy"){                //적에게 닿을 시
            other.GetComponent<Enemy>().Damaged(bulletDamage);
        }
    }

    void RigidBodyBlink(){
        if(boxCol.enabled == true){
            boxCol.enabled = false;
        }else{
            boxCol.enabled = true;
        }
        Invoke("RigidBodyBlink", blinkTime);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////Destroy

    void EndAtkStart(){
        endAtk = true;
    }

    void DestroyAnim(){
        if(endAtk == true){
            transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y - 0.05f);
            if(transform.localScale.y <= 0){ 
                Destroy();
            }
        }
    }

    private void Destroy(){
        Destroy(gameObject);
    }
}