- Home /
Problem with mobs
Hello, I'll expose my problem: I'm doing a 2D platform game, and I have ennemies that shoot you when they see you but , only the first one shoots you and the rest doesn't do anything. I'm a bit noob scripting so I've done a very basic AI and maybe is not the best way to do it :P If you can tell me why only the first one shoots me it would be fantastic.
There are the scripts:
using UnityEngine;
using System.Collections;
public class Mob : MonoBehaviour {
public GameObject player;
public GameObject laser;
public AudioClip a_laser;
public AudioClip exterminar;
private GameObject mob;
private bool attack;
private float time;
private AudioSource audio;
// Use this for initialization
void Start () {
mob = gameObject;
audio = GetComponent<AudioSource> ();
}
// Update is called once per frame
void FixedUpdate () {
if (Vector2.Distance (player.transform.position, mob.transform.position) < 10) {
attack = true;
}
if (attack) {
time += Time.deltaTime;
if (time>= 1) {
Invoke ("Ataca",0);
time = 0;
}
}
}
void Ataca () {
audio.clip = a_laser;
audio.Play ();
Instantiate (laser, this.transform.position, Quaternion.identity);
// Debug.Log ("ataaaacaaaaa");
}
void OnCollisionEnter2D (Collision2D coll) {
if (coll.gameObject.tag == "Piruleta") {
Destroy (this.gameObject);
}
}
}
And the laser prefab script:
using UnityEngine;
using System.Collections;
public class Laser : MonoBehaviour {
public Vector2 laser;
public AudioClip a_muerte;
private AudioSource audio;
private Rigidbody2D rb;
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource> ();
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void FixedUpdate () {
rb.AddForce (new Vector3(-0.1f,0,0));
}
void OnTriggerEnter2D (Collider2D coll) {
if (coll.gameObject.tag != "Player") {
Destroy (this);
}
else {
Debug.Log ("laser.toca");
audio.PlayOneShot (a_muerte,0.5f);
Application.LoadLevel ("GameOvah");
Destroy (this);
}
}
}
Thank you very much for your attention!
Comment
is this script on each of your $$anonymous$$ob gameobjects?