본문 바로가기
개발/C#

유니티 C# 총알 발사하기 간단 구현 bullet Shot - 슈팅게임

by SPNK 2023. 1. 29.
반응형

코드 작성

using UnityEngine;

public class BulletFiring : MonoBehaviour
{
    public GameObject bulletPrefab;
    public Transform bulletSpawn;
    public float bulletSpeed = 20f;
    public float fireRate = 0.5f;
    private float nextFire;
    
    void Update()
    {
    	// 0.5초 간격으로 총알을 발사 할 수 있음
        if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Fire();
        }
    }

    void Fire()
    {
        // 총알 프리팹 생성
        GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);

        // 총알 발사
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletSpeed;

        // 2초 뒤에 파괴
        Destroy(bullet, 2.0f);
    }
}

 


참고할만한 글

 

유니티 C# 2D 물리 점프 간단 구현 Unity Rigidbody2D Jump

코드 작성 using UnityEngine; public class PlayerController : MonoBehaviour { public float jumpForce = 10f; public float moveSpeed = 5f; public float groundCheckRadius = 0.2f; public LayerMask whatIsGround; public Transform groundCheck; private Rigidbod

parksh3641.tistory.com

 

유니티 C# 캐릭터 키보드 이동 간단 구현

캐릭터 키보드 이동 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerMove : MonoBehaviour { public float moveSpeed = 5.0f; private void FixedUpdate() //키보드로 이동 { floa

parksh3641.tistory.com

 

반응형

댓글