Unity/수업 내용

[Unity 2020.3] Shooting Project 플레이어의 공격 오브젝트 Bullet

JSH1 2021. 10. 14. 15:50

 


Bullet 스크립트 생성

using UnityEngine;

public class Bullet : MonoBehaviour
{
    PlayerShoot playerShoot;
    public int damage;

    void Awake()
    {
        playerShoot = GameObject.Find("Player").GetComponent<PlayerShoot>();
    }

    void Update()
    {
        transform.Translate(Vector3.up * 10 * Time.deltaTime);

        if (transform.position.y > Camera.main.orthographicSize + 1)
        {
            BulletDestroy();
        }
    }

    public void BulletDestroy()
    {
        gameObject.SetActive(false);
        playerShoot.BulletDestroy(gameObject);
    }
}

Main하고 Sub Bullet 오브젝트에 Bullet 스크립트를 추가

Box Collider 2D를 추가하고 Is Trigger를 체크하고
Rigidbody 2D도 추가한 다음에 Body Type을 Kinematic으로 변경