Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion Bullet not spawning at BulletSpawnPosition

Discussion in 'Prefabs' started by PHOENIX05102000, Nov 17, 2022.

  1. PHOENIX05102000

    PHOENIX05102000

    Joined:
    Oct 14, 2022
    Posts:
    16
    Hi, I am trying to make the enemy shoot the player when he comes in sight. But no matter what I do, the bullet is not at all spawning. Could someone help me with this. Please note that bullet spawn position in some distance in front of the barrel.

    EnemyWeapon Manager Script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyWeaponManager : MonoBehaviour
    6. {
    7.     [HideInInspector] public bool bIsReadyToFire;
    8.     public Transform WeaponTransform;
    9.     public Player player;
    10.     public Enemy enemy;
    11.     [SerializeField] private float WeaponShootingForce;
    12.     [SerializeField] private float FireRate;
    13.     public Transform BulletSpawn;
    14.     private WaitForSeconds WeaponWaitDuration;
    15.     public GameObject BulletGameObject;
    16.  
    17.  
    18.  
    19.     private void Awake()
    20.     {
    21.         bIsReadyToFire = false;
    22.     }
    23.  
    24.     private void Start()
    25.     {
    26.         WeaponWaitDuration = new WaitForSeconds(1 / FireRate);
    27.     }
    28.  
    29.     public void Update()
    30.     {
    31.         IsEnemyReadyToFire();
    32.         Fire();
    33.         //Debug.Log("Is Ready To Fire: " + bIsReadyToFire);
    34.     }
    35.  
    36.     public bool IsEnemyReadyToFire()
    37.     {
    38.         RaycastHit PlayerHit;
    39.         if(bIsReadyToFire == false && Physics.Raycast(WeaponTransform.position, transform.forward, out PlayerHit, 3.0f))
    40.         {
    41.             player = PlayerHit.collider.GetComponentInParent<Player>();
    42.             if(player)
    43.             {
    44.                 bIsReadyToFire = true;
    45.             }
    46.         }
    47.         if(bIsReadyToFire == true && !Physics.Raycast(WeaponTransform.position, transform.forward, out PlayerHit, 3.0f))
    48.         {
    49.             bIsReadyToFire = false;
    50.         }
    51.  
    52.         return bIsReadyToFire;
    53.     }
    54.  
    55.     public IEnumerator Fire()
    56.     {
    57.         if(bIsReadyToFire == true && enemy.bIsPlayerInSight == true)
    58.         {
    59.             //bullet = Instantiate(bullet, BulletSpawn.position, BulletSpawn.rotation);
    60.             //bullet.BulletInitilization(this);
    61.             //bullet.BulletGameObject.GetComponent<Rigidbody>().AddRelativeForce(BulletSpawn.forward * WeaponShootingForce, ForceMode.Impulse);
    62.             Instantiate(BulletGameObject, BulletSpawn.position, BulletSpawn.rotation).GetComponent<Rigidbody>().AddRelativeForce(BulletSpawn.forward * WeaponShootingForce);
    63.             yield return WeaponWaitDuration;
    64.         }
    65.  
    66.     }
    67.  
    68. }
    69.  
    BulletScript:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.     [SerializeField] private float BulletLifetime;
    8.     [HideInInspector] public Player player;
    9.  
    10.     private void Awake()
    11.     {
    12.         Destroy(this, BulletLifetime);
    13.     }
    14.  
    15.     public void OnTriggerEnter(Collider other)
    16.     {
    17.         Destroy(this);
    18.         if (other.tag == "Player")
    19.         {
    20.             player.PlayerHealth -= 20.0f;
    21.         }
    22.     }
    23.  
    24.  
    25. }
    26.  
    EnemyWeaponManager in Editor:

    upload_2022-11-17_13-7-24.png
     

    Attached Files:

  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    add more Debug.Logs to see what methods get called or not.

    atleast ienumerator method probably needs to be called with:
    StartCoroutine(Fire());

    but calling it inside update loop, with delay inside, would mean it runs new coroutine every frame..
    so maybe you want to call it at Start() once, and make it endless loop with:
    Code (CSharp):
    1. while(true)
    2. {
    3. .. your current code..
    4. yield return 0;
    5. }