Q&A

fps2

작성자 : 진영훈

작성일 : 2023-12-08 13:53


using UnityEngine; public class PlayerShooting : MonoBehaviour { public GameObject bulletPrefab; // 총알 프리팹 public Transform firePoint; // 발사 지점 public float bulletSpeed = 10f; // 총알 속도 void Update() { // 마우스 왼쪽 버튼을 누르면 총알 발사 if (Input.GetButtonDown("Fire1")) { Shoot(); } } void Shoot() { // 총알을 생성하고 발사 지점에서 발사 GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); // 총알에 속도 적용 Rigidbody bulletRb = bullet.GetComponent<Rigidbody>(); bulletRb.AddForce(firePoint.forward * bulletSpeed, ForceMode.VelocityChange); } }