- Home /
Destroy Turret with machine Gun
Hello.
I have worked on the raycast collider scripts for a few days now, trying to figure out how to allow A (particle emitter) Machine gun script to detect the enemy turret I've set up, which in this see, be able to damage the turret upon contact with the bullet Impact emitter.
When the health of the enemy turret is 0, it destroys itself while causing a small explosion(the explosion prefab from Unity)
However, I've tried to search for several answers to my detection problem, and asked for help from a fellow friend. After several alterations, this is what I have come up with:
[heliMachineGunScript]
var gun_Emitter_Object : GameObject;
var weapon_Fire_Delay : float = 0.0; //this cant be 0!! This var is the delay between the shots
var bullet_Impact_Prefab : GameObject;
var emitter_life : float = 0.1; //the time before the impact prefab gets destroyed
private var weapon_timer : float; //timer
private var bullet : GameObject; //the current bullet we have just shooted
function Start ()
{
weapon_timer = Time.time + weapon_Fire_Delay; //this resets the timer
}
function Update ()
{
gun_Emitter_Object.particleEmitter.emit = false;
if ( Input.GetButton( "Fire1" ) && Time.time >= weapon_timer)
{
gun_Emitter_Object.audio.Play();
gun_Emitter_Object.particleEmitter.emit = true;
var hit : RaycastHit;
if ( Physics.Raycast( gun_Emitter_Object.transform.position, gun_Emitter_Object.transform.forward, hit, Mathf.Infinity) )
{
bullet = Instantiate( bullet_Impact_Prefab, hit.point, Quaternion.LookRotation( hit.normal ) );
if(hit.collider.gameObject.tag == "Enemy" || hit.collider.gameObject.tag == "enemy"){
hit.collider.gameObject.GetComponent(enemyTurretDestroyScript).MachineGun();
Debug.Log("YES");
}
}
weapon_timer = Time.time + weapon_Fire_Delay;
}
}
[enemyTurretDestroyScript]
var turretHealth : float;
var explosion_Prefab : GameObject;
function Update()
{
if (turretHealth < 1)
{
Instantiate( explosion_Prefab, transform.position, transform.rotation );
Destroy(gameObject);
}
}
function MachineGun ()
{
turretHealth -= 1;
}
It still doesn't work, nor does it detects the turret when I fire at it.
Addtional notes to know: The Machine Gun is not a projectile based weapon but a particle emitter weapon, with a heli_Gun_Emitter that is active when fired, and the bulletimpact Prefab will appear when the MG script detects a collision on any mesh.
The turret has the "Enemy" tag.
Could someone help me on this script? I'm in a rush to get things done, and would gladly appreciate any pointers from the fellow unity members.
If I'm not clear enough, let me know.
Your answer
Follow this Question
Related Questions
Apply opposite force on mouse-click + collision 2 Answers
How to Destroy an Object while bullet is colliding it 2 Answers
Objects do not destroy on Collision 1 Answer
Gun Script 1 Answer
How to Destroy a gameobject on collision 3 Answers