fbpx

 

Enemy fires itself
 

Enemy fires itself  

   RSS

0

I'm trying to make the enemy ships fire at the player under three seconds but the enemy shoots itself and the lazer fires like a beam not like individual bullets .

this is the enemy script:

public class enemy : MonoBehaviour
{
[SerializeField]
private float _moveSpeed = 1.5f;
private Player _player;
private Animator _boom;
[SerializeField]
private AudioSource _source;
[SerializeField]
private AudioClip _enemyboom;
[SerializeField]
private GameObject _lazer;
[SerializeField]
private float _lazerspeed = 0.5f ;
private float _canfire = -1;
void Start()
{
_player = GameObject.Find("Player").GetComponent<Player>();
_source = GetComponent<AudioSource>();
if(_player == null)
{
Debug.LogError("The PLAYER is NULL");
}
_boom = GetComponent<Animator>();
if (_boom == null)
{
Debug.LogError("The animator is NULL");
}
if(_source == null)
{
Debug.LogError("The Audio Source is Null");
}
else
{
_source.clip = _enemyboom;
}
}


// Update is called once per frame
void Update()
{
transform.Translate (Vector3.down * _moveSpeed * Time.deltaTime);
Respawn();
StartCoroutine(EnemyFire());
}
private void Respawn()
{
float xrand = Random.RandomRange(-7f,7f);


//moves 4 meters persecond
if(transform.position.y < -7.4f)
{
//respawn from top, possibly in random position
transform.position= new Vector3(xrand, 7, 0);
}
}

private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag =="Player")
{
Player player = other.transform.GetComponent<Player>();//calls the method from player script
if(player != null) //cheks if the method is really there this is called null check
{
player.Damage();//destroys player
}

_boom.SetTrigger("OnEnemyDeath");
_moveSpeed = 0;
_source.Play();
Destroy(this.gameObject, 2.8f);



}
if (other.gameObject.CompareTag("Lazer"))
{
//lazer is destroyed first
Destroy(other.gameObject);
//add 10 to score
if (_player != null)
{
_player.Score(10);

}
_boom.SetTrigger("OnEnemyDeath");
_moveSpeed = 0;
_source.Play();
Destroy(GetComponent<Collider2D>());
Destroy(this.gameObject, 2.8f);


}
}

IEnumerator EnemyFire()
{
_canfire = Time.time + _lazerspeed;
while (true)
{
Instantiate(_lazer, transform.position + new Vector3(0, -2.98f, 0), Quaternion.identity);
yield return new WaitForSeconds(3.0f);

}

}




}


this is the enemy lazer script: using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemylazer : MonoBehaviour
{
[SerializeField]
private float _speed = 8.0f;
// Start is called before the first frame update


// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
Destroyed();
}
void Destroyed()
{
//limit is 7.2 in Y axis
if (transform.position.y < -8.0f)
{
if (transform.parent != null)
{
Destroy(transform.parent.gameObject);
}
Destroy(gameObject);
}
}
}
0

Do you have your enemy lasers tagged as Lazer? Because that is what the enemy script is checking when anything enters its collider. And its lasers start in its collider so they get checked. 

@thom-foxx

I did , I changed the tag to "enemy lazer" now the problem is that the lazers are not firing down at the player , one is shooting down and the other is firing back at the enemy, sorry I'm new to this

Share:

 

 

Scroll to Top

Please Login or Register