데브허브 | DEVHUB | How to Make Shooting in Unity with C# for Beginners | Unity 6 TutorialHow to Make Shooting in Unity with C# for Beginners | Unity 6 Tutorial
PlayerShoot 스크립트를 생성하여 플레이어 게임 오브젝트에 연결하고 슈팅 로직을 구현합니다. 🔗
Update 함수 내에서 Input.GetMouseButtonDown(0)을 사용하여 왼쪽 마우스 버튼 클릭을 감지합니다. 🖱️
void Shoot() 함수를 정의하여 슈팅 관련 동작을 캡슐화하고, 초기에는 디버그 메시지로 테스트합니다. 🎯
- 총알 스프라이트 시트를 가져와
Sprite Mode를 Multiple로, Filter Mode를 Point (no filter)로 설정하고 Sprite Editor로 개별 총알을 슬라이스합니다. 🖼️
- 선택한 총알 스프라이트에
Rigidbody2D (Body Type: Kinematic, Collision Detection: Continuous)와 BoxCollider2D (Is Trigger 활성화)를 추가한 후 프리팹으로 만듭니다. 🧱
- 플레이어의 자식으로 빈 게임 오브젝트
ShootPoint를 생성하고, 총알이 발사될 위치에 배치합니다. 📍
PlayerShoot 스크립트에 public GameObject bulletPrefab과 public Transform shootPoint 변수를 선언하고, Instantiate(bulletPrefab, shootPoint.position, Quaternion.identity)를 사용하여 총알을 생성합니다. ✨
- 생성된 총알을
tempBullet 변수에 저장하고, tempBullet.GetComponent<Rigidbody2D>().linearVelocity에 public float bulletVelocity 값을 설정하여 총알이 움직이도록 합니다. 🚀