using System; using System.Collections; using System.Collections.Generic; using…
- User
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Shotgun : MonoBehaviour { [Header("Resources")] [SerializeField] private LayerMask canBeShot; [SerializeField] private GameObject bulletHolePrefab; [SerializeField] private float shotDistance; [Header("Shotgun Parameters")] [NonSerialized] private bool isReloading; [SerializeField] private float fireRate = 2f; [SerializeField] private int pellets = 10; [SerializeField] private float bloom = 0.15f; [NonSerialized] private float currentCooldown; [SerializeField] private int maxAmmo = 6; [NonSerialized] private int currentAmmo; [SerializeField] private float reloadTime; private void Start() { currentAmmo = maxAmmo; isReloading = false; } private void Update() { if (Input.GetKey(KeyCode.Mouse0) && Time.time >= currentCooldown && !isReloading && currentAmmo > 0) { Shoot(); } else if ((currentAmmo < maxAmmo && Input.GetKeyDown(KeyCode.R) && !isReloading) || currentAmmo == 0) { StartCoroutine(Reload()); }
} private void Shoot() { currentCooldown =Time.time + fireRate;Vector3 shotOrigin = Camera.main.transform.position; Vector3 shotDirection = Camera.main.transform.forward; for (int pelletIndex = 0; pelletIndex < pellets; pelletIndex++) { Vector3 randomSpread = new Vector3(UnityEngine.Random.Range(-bloom, bloom), UnityEngine.Random.Range(-bloom, bloom)); Vector3 finalShotDirection = shotDirection + randomSpread; RaycastHit hitInfo; if (Physics.Raycast(shotOrigin, finalShotDirection, out hitInfo, shotDistance, canBeShot)) { Vector3 hitPoint = hitInfo.point; Quaternion hitRotation = Quaternion.LookRotation(hitInfo.normal); GameObject bulletHole = Instantiate(bulletHolePrefab, hitPoint, hitRotation); bulletHole.transform.position += hitInfo.normal * 0.01f; Destroy(bulletHole, 2f); } } currentAmmo--; } private IEnumerator Reload() { isReloading = true; yield return new WaitForSeconds(reloadTime); currentAmmo = maxAmmo; isReloading = false; }} Есть идеи почему префаб появляется в заднице, а не на поверхности стены?
- Assistant
Ваша проблема, связанная с появлением префаба (в данном случае, bullet hole) "в заднице" (или в непредсказуемом месте), скорее всего, связана с тем, что вы неправильно обрабатываете направление, в котором происходит выстрел, или с тем, как вы настраиваете позицию префаба на поверхности.