so i have three enemies in a scene that draw rays and if the player is hit by that ray they should shoot a bullet. the thing is only the first object to hit the player will shoot if two enemies hit the player the enemy that seen the player first will shoot two bullets. any help is greatly appreciated
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TyphlosisOfFaith : MonoBehaviour
{
public int health;
public float speed, closeRange, attackRange, maxRange;
public Player player;
GameObject sphere;
[SerializeField] private GameObject bulletPrefab;
private GameObject bullet;
Transform bSpawn;
public Material coreBaseMat, coreDangerMat;
Transform core;
// Use this for initialization
private void Awake ()
{
player = GameObject.Find("Player").GetComponent<Player>();
health = 200;
maxRange = 75;
attackRange = 10;
closeRange = 5;
speed = 10;
bSpawn = GameObject.Find("bulletSpawn").transform;
}
// Update is called once per frame
private void Update ()
{
if (this.bullet != null)
{
this.bullet.transform.Translate(Vector3.forward * 50 * Time.deltaTime);
}
Vector3 target = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt(player.transform);
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, 0.5f, out hit))
{
if (hit.transform.tag == "Player" )
{
bullet = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Sphere), bSpawn.position, bSpawn.rotation);
//bullet.transform.parent = transform;
StartCoroutine(DestroyOnTime(.5f));
//bullet.transform.parent = null;
}
}
if (Vector3.Distance(transform.position, player.transform.position) < attackRange && Vector3.Distance(transform.position, player.transform.position) > closeRange)
{
// transform.Translate(transform.forward * Time.deltaTime * speed);
}
else if(Vector3.Distance(transform.position, player.transform.position) < closeRange)
{
core = transform.GetChild(1);
core.GetComponent<Renderer>().material = coreDangerMat;
}
}
IEnumerator DestroyOnTime(float time)
{
yield return new WaitForSecondsRealtime(time);
Destroy(this.bullet);
}
}
i have tried creating a script that places all objects in the scene rather than using the gui, still does it. is it something to do with the fact all the prefabs are operating with the same raycast and if so how do i stop it. read something a week or so back that was talking about making a variable static all instances of that prefab will share the value variable but no var is static? also does static not mean that only one instance of the object can be created. also the script is a bit messy because i was trying to fix the problem.