Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Multiple ennemies with the same AI script

$
0
0
Hello, I have a problem : The oponents are instantiated from a single prefab (A Map prefabe that has ennemies). These enemies has a script that makes them move and fire (I'm in 2D). But they all are doing the same thing, If my character is at the left of one enemy, they're all going and firing to the left. I don't know how to unlink all these ennemies. Can you help me please? I saw many topics but i don't understand the answer... (create a new script in the ennemy calling the IA script?) Here is the script attached to the enemy : using UnityEngine; using System.Collections; public class Enemy : MonoBehaviour { private float life = 3f; private float timer; public Rigidbody2D enemybulletprefab; private float attackspeed = 0.05f; float cooldown; private float range = 2f; private float xPlayer ; private float yPlayer; private float xEnemy; private float yEnemy; private float speed = 4f; // the speed that the player will move private Vector3 movement; private float lv; private float lh; private Rigidbody2D EnnemyRigidbody2D; // Use this for initialization void Awake() { //Get references EnnemyRigidbody2D = GetComponent (); } void FixedUpdate () { //Store input axes xPlayer = GameObject.FindGameObjectWithTag("Player").transform.position.x; yPlayer = GameObject.FindGameObjectWithTag("Player").transform.position.y; xEnemy = GameObject.Find ("Enemy").transform.position.x; yEnemy = GameObject.Find("Enemy").transform.position.y; // print("Player" + xPlayer + " et " + yPlayer); // print("Enemy" + xEnemy + " et " + yEnemy); if (xPlayer < xEnemy) { lv = -1; } else { lv = 1;} if (yPlayer < yEnemy) { lh = -1; } else {lh = 1;} Move (lv,lh); } void Move(float lh,float lv) { // move the player movement.Set (lh, lv, lv); movement = movement.normalized * speed * Time.deltaTime; EnnemyRigidbody2D.MovePosition (transform.position + movement); } void Update () { if (Time.time >= cooldown) { Fire(); } timer += 1.0F * Time.deltaTime; } void Fire(){ Rigidbody2D ebPrefab = Instantiate (enemybulletprefab, transform.position, Quaternion.identity) as Rigidbody2D; xPlayer = GameObject.FindGameObjectWithTag("Player").transform.position.x; yPlayer = GameObject.FindGameObjectWithTag("Player").transform.position.y; xEnemy = GameObject.Find ("Enemy").transform.position.x; yEnemy = GameObject.Find("Enemy").transform.position.y; float xDif = xPlayer - xEnemy; float yDif = yPlayer - yEnemy; ebPrefab.AddForce (Vector3.up * yDif * 100); ebPrefab.AddForce (Vector3.right * xDif * 100); cooldown = Time.time + attackspeed; } void OnTriggerEnter2D(Collider2D other) { //print (other); //print (other.GetComponent ().name); if (other.GetComponent().name == "perso" || other.GetComponent().name == "Fire(Clone)" ) { life = life - 1; if(life == 0) { Destroy (gameObject); } } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>