Hi guys,
I am a bit new to scripting, but I am trying the best I can with the little knowledge I got. I am making a simple tank shooting game in C#. Just when I thought I was done with all the mechanics I am encountering a new problem.
I only have one enemy tank in the scene, and it is able to look at the Player position and shoot at it whenever the player gets in “range” (I am using triggers for this. But once I duplicate the original enemy tank, only the new one would detect the player on it’s range. unless the duplicated enemy tank detects it.
This is the BulletSpawnerScript I created for the enemy. It is attached to a “bulletSpawner” gameObject on the tip of the cannon, and checks for another script that is attach to a trigger (Range).
I would appreciate if someone can tell me what am I doing wrong and what can I do to fix it. In case my English is not good enough, I am also attaching an image. Thanks
public class EnemyBulletSpawnScript : MonoBehaviour {
public Rigidbody bullets;
public float bulletSpeed = 50;
public AudioClip ShootSound;
public bool enemyFireNow = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (GameObject.Find("ShootingRange").GetComponent <ShootingRange> ().fireNow)
{
enemyFireNow = true;
GetComponent<AudioSource>().PlayOneShot(ShootSound);
Rigidbody spawnedBullet = Instantiate(bullets, transform.position, transform.rotation) as Rigidbody;
spawnedBullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
}
if (GameObject.Find("InnerShootingRange").GetComponent <ShootingRange> ().fireNow)
{
enemyFireNow = true;
GetComponent<AudioSource>().PlayOneShot(ShootSound);
Rigidbody spawnedBullet = Instantiate(bullets, transform.position, transform.rotation) as Rigidbody;
spawnedBullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
}
if (GameObject.Find("CloseShootingRange").GetComponent<ShootingRange>().fireNow)
{
enemyFireNow = true;
GetComponent<AudioSource>().PlayOneShot(ShootSound);
Rigidbody spawnedBullet = Instantiate(bullets, transform.position, transform.rotation) as Rigidbody;
spawnedBullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
}
else
{
enemyFireNow = false;
}
}
}