• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by patosalas · May 20, 2017 at 09:13 AM · c#script.duplicateproblema

Only the last clone of my enemies can harm me, they all have the same script

Hi, thank for all your help in advance, I´ll keep it simple:

I have 3 enemies ( cloned objects) with the same script attached, they all behave the same way, they detect me, and chase me and want to attack me , just as I coded. Here´s the thing...."Only the last clone of my original enemy can harm me, the others attack me, but my player just doesn´t play the "being hurt" animation. To make it clearer....

when I had these enemies: reptil, reptil (1), reptil (2) .....only reptil (2) could harm me Now I have these enemies: reptil, reptil(1), reptil (2), reptil (3) .....only reptil (3) can harm me now

alt text

alt text

PLAYER SCRIPT

 using UnityEngine;
 using System.Collections;
 
 public class leono : MonoBehaviour {
 
     private leono leonoControl;
     public Animator animador;
 
 
     public float salto;
     public float zancada;
 
     private float fuerzaSalto;
     private float fuerzaZancada;
 
     public float timerComboLeono;
     private float segundosComboLeono;
     private bool der = false;
     private bool izq = false;
     private bool enTierra = true;
     private bool saltando = false;
     private bool agachado = false;
     private bool dobleSaltoPosible;
     public bool ataqueEnTierra ;
 
     //GameObject reptilBasico;
 
 
     public bool siendoGolpeado;
     public bool golpeando;
     public bool leonoCombo;
 
     private reptil1 reptil1;
 
     void Awake () {
         animador = GetComponent<Animator>();  
     }
     // Use this for initialization
     void Start () {
 
         leonoControl = FindObjectOfType<leono> ();
         reptil1 = FindObjectOfType<reptil1> ();
     //    reptilBasico =  GameObject.FindGameObjectWithTag ("enemigo");
     
     }
 
 
 
 
     public bool animHerido(){
         return this.animador.GetCurrentAnimatorStateInfo (0).IsName ("leono golpeado");
     }
 
     bool reposoTierra(){
         return !der && !izq && !agachado && enTierra && !saltando  &&!siendoGolpeado && fuerzaSalto == 0;
     }
 
     bool ataqueTierra(){
         return this.animador.GetCurrentAnimatorStateInfo (0).IsTag ("ataque en tierra");
     }
     bool ataqueAire(){
         return this.animador.GetCurrentAnimatorStateInfo (0).IsTag ("golpe en salto");
     }
 
     void OnTriggerEnter2D(Collider2D col) {
         //if (col.gameObject.tag == "plataforma roca") {
             enTierra = true;
             saltando = false;
             Debug.Log ("en tierra");
         //}
     }
 
     void OnTriggerExit2D(Collider2D col) {
         //if (col.gameObject.tag == "plataforma roca") {
             enTierra = false;
             Debug.Log ("saltando");
         //}
     }
 
 
 
 
     void Update () {
 
 
         fuerzaZancada = 0f;
         fuerzaSalto = GetComponent<Rigidbody2D> ().velocity.y;  
 
         if (this.animador.GetCurrentAnimatorStateInfo (0).IsTag ("ataque en tierra")) {
             ataqueEnTierra = true;
         }
         if (!this.animador.GetCurrentAnimatorStateInfo (0).IsTag ("ataque en tierra")) {
             ataqueEnTierra = false;
         }
 
         if (this.animador.GetCurrentAnimatorStateInfo (0).IsName ("leono golpeado")) {
             siendoGolpeado= true;
             agachado = false;
 
 
             if (reptil1.transform.localScale.x == -1) {
                 transform.localScale = new Vector2 (1,1);
                 fuerzaZancada = - 2f;
             }
             if (reptil1.transform.localScale.x == 1) {
                 transform.localScale = new Vector2 (-1,1);
                 fuerzaZancada =  2f;
             }
         }
 
 
         if (!this.animador.GetCurrentAnimatorStateInfo (0).IsName ("leono golpeado")) {
             siendoGolpeado = false;
         }
 
     
 
         //LEONO GOLPEADO
 
         if ( reptil1.golpeando && !animHerido()
             && reptil1.transform.position.x < transform.position.x + reptil1.rangoCombate 
             && reptil1.transform.position.x > transform.position.x - reptil1.rangoCombate
         ) {
             
             siendoGolpeado = true;
             animador.SetTrigger ("leono golpeado");
 
         }
 
         //if (!animHerido ()) {
         //    siendoGolpeado = false;
         //}
 
 
 
         //ATAQUE EN AIRE
         if(Input.GetKeyDown(KeyCode.S)&& !enTierra && !ataqueAire()  &&!siendoGolpeado ){
             animador.SetTrigger ("golpe en salto");
 
             if ( !reptil1.siendoGolpeado && !reptil1.golpeando && transform.position.y < reptil1.transform.position.y + 2F 
                 && reptil1.transform.position.x < transform.position.x + reptil1.rangoCombate 
                 && reptil1.transform.position.x > transform.position.x - reptil1.rangoCombate
                 &&transform.localScale.x != reptil1.transform.localScale.x
             ) {
                 reptil1.animador.SetTrigger ("reptil 1 herido");
             }
         }
 
 
         if (leonoCombo && !ataqueTierra() && !siendoGolpeado) {
             segundosComboLeono -= Time.deltaTime;
             Debug.Log ("timer comenzo");
         }
         if (Input.GetKeyDown (KeyCode.S) && leonoCombo && !ataqueTierra()&& fuerzaSalto == 0 && !saltando && !agachado && !animHerido()) {
             animador.SetTrigger ("golpe combo");
             ataqueEnTierra = true;
             if (reptil1.COMBATE == true && !reptil1.golpeando && transform.position.y < reptil1.transform.position.y + 0.1F
                 &&transform.localScale.x != reptil1.transform.localScale.x) {
                 reptil1.animador.SetTrigger ("reptil 1 herido");
 
             }
         }
         if (segundosComboLeono <= 0) {
             leonoCombo = false;
             segundosComboLeono = timerComboLeono;
         }
         //ATAQUE SIMPLE
         if(Input.GetKeyDown(KeyCode.S)&& !leonoCombo && !ataqueTierra () && enTierra && !agachado && !saltando && fuerzaSalto == 0 && !siendoGolpeado){
             animador.SetTrigger ("golpe simple");
             ataqueEnTierra = true;
             leonoCombo = true;
             if (reptil1.COMBATE == true && !reptil1.siendoGolpeado && !reptil1.golpeando && transform.position.y < reptil1.transform.position.y + 0.1F
                 &&transform.localScale.x != reptil1.transform.localScale.x) {
                 reptil1.animador.SetTrigger ("reptil 1 herido");
 
             }
         }
             
 
         //AGACHADO
         if(Input.GetKey(KeyCode.DownArrow) && enTierra && !siendoGolpeado ){
             agachado = true;
         }
 
         if (agachado) {
             fuerzaZancada = 0;
             animador.SetInteger ("leono", 3);
             if (Input.GetKeyDown (KeyCode.S) && agachado && !ataqueTierra() && !siendoGolpeado) {
                 animador.SetTrigger ("golpe bajo");
                 if (reptil1.COMBATE == true && !reptil1.siendoGolpeado && !reptil1.golpeando 
                     && transform.position.y < reptil1.transform.position.y + 0.1F
                     &&transform.localScale.x != reptil1.transform.localScale.x
                 ) {
                     reptil1.animador.SetTrigger ("reptil 1 herido");
 
                 }
             }
         }
 
         if  (Input.GetKeyUp (KeyCode.DownArrow)){
             agachado = false;
         }
 
         //HACIA LA DERECHA
 
         if (Input.GetKey (KeyCode.RightArrow)&& !izq && !ataqueTierra() && !siendoGolpeado) {
             der = true;
             transform.localScale = new Vector3 (1f, 1f, 1f);
 
             if (!enTierra) {
                 fuerzaZancada = zancada ;
             }
 
             if (enTierra && !ataqueTierra() && !agachado) {
                 fuerzaZancada = zancada;
                 animador.SetInteger ("leono", 1);
             }
         }
         if (Input.GetKeyUp (KeyCode.RightArrow)) {
             der = false;
         }
 
 
 
 
         //HACIA LA IZQUIERDA
 
 
         if (Input.GetKey (KeyCode.LeftArrow)&& !der && !ataqueTierra() && !siendoGolpeado) {
             izq = true;
             transform.localScale = new Vector3 (-1f, 1f, 1f);
 
             if (!enTierra) {
                 fuerzaZancada = -zancada ;
             }
             if (enTierra && !ataqueTierra() && !agachado) {
                 fuerzaZancada = -zancada;
                 animador.SetInteger ("leono", 1);
             }
         }
 
         if (Input.GetKeyUp (KeyCode.LeftArrow)) {
             izq = false;
         }
 
         //SALTO
         if(Input.GetKeyDown(KeyCode.D) && enTierra && !ataqueTierra() && !ataqueEnTierra && !agachado && !saltando && !siendoGolpeado){
             saltando = true;
 
             fuerzaSalto = salto;
 
 
         }
         if (!enTierra && fuerzaSalto >= 1) {
             animador.SetInteger ("leono",4);
         }
         if (!enTierra && fuerzaSalto < 1 && fuerzaSalto > -1) {
             animador.SetInteger ("leono",5);
         }
         if (!enTierra && fuerzaSalto <= -1 && !der && !izq) {
             animador.SetInteger ("leono",6);
         }
         if (!enTierra && fuerzaSalto <= -1 && fuerzaZancada != 0) {
             animador.SetInteger ("leono", 7);
             Debug.Log ("caca");
         }
 
 
         //REPOSO TIERRA
         if (reposoTierra()){
             animador.SetInteger ("leono", 0);
         }
 
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (fuerzaZancada, fuerzaSalto);
 
     }
 }
 

ENEMIE´S SCRIPT

using UnityEngine; using System.Collections;

public class reptil1 : MonoBehaviour {

 public float velocidadTranco;
 public float rangoPersecucion;
 public float rangoCombate;
 private float velHorizontal;
 private float velVertical;
 private float tiempoDuranteElCualMePuedenGolpear ;
 public float segundosDuranteElCualMePuedenGolpear;



 public float alturaPersecucion;
 public float alturaCombate;

 private reptil1 reptil;
 public Animator animador;
 private leono leono;


 private bool IDLE = true;
 public bool COMBATE;

 public bool siendoGolpeado = false;
 public bool golpeando;
 private bool PERSECUCION;
 private bool alturaPerOK;
 private bool alturaComOK;


 void Awake(){

     animador = GetComponent<Animator> ();

 }

 // Use this for initialization
 void Start () {

     leono = FindObjectOfType<leono> ();
     reptil = FindObjectOfType<reptil1> ();

 }




 bool atacando(){
     return this.animador.GetCurrentAnimatorStateInfo (0).IsName ("reptil 1 ataque");
 }


 public void siendoAtacado(){
     siendoGolpeado = true;
     Debug.Log ("siendo golpeado");
 }
 public void noSiendoAtacado(){
     siendoGolpeado = false;
     Debug.Log ("no siendo golpeado");
 }



 // Update is called once per frame
 void Update () {

     if (this.animador.GetCurrentAnimatorStateInfo (0).IsName ("reptil 1 herido")) {
         siendoGolpeado= true;
     }
     if (!this.animador.GetCurrentAnimatorStateInfo (0).IsName ("reptil 1 herido")) {
         siendoGolpeado = false;
     }
     if (this.animador.GetCurrentAnimatorStateInfo (0).IsName ("reptil 1 ataque")) {
         golpeando= true;
     //    Debug.Log ("caca");
     }
     if (!this.animador.GetCurrentAnimatorStateInfo (0).IsName ("reptil 1 ataque")) {
         golpeando= false;
     }






     if (leono.transform.position.x < transform.position.x) {
         transform.localScale = new Vector2 (-1, 1);
     }

     if (leono.transform.position.x > transform.position.x) {
         transform.localScale = new Vector2 (1, 1);
     }


     //ALTURA PERSECUCION
     if (
         leono.transform.position.y < transform.position.y + alturaPersecucion 
         || leono.transform.position.y > transform.position.y - alturaPersecucion ){
         alturaPerOK = true;
     }

     if (leono.transform.position.y > transform.position.y + alturaPersecucion
         || leono.transform.position.y < transform.position.y - alturaPersecucion) {
         alturaPerOK = false;
     }

     //ALTURA COMBATE
     if (
         leono.transform.position.y < transform.position.y + alturaCombate
         || leono.transform.position.y > transform.position.y - alturaCombate ){
         alturaComOK = true;
     }

     if (leono.transform.position.y > transform.position.y + alturaCombate
         || leono.transform.position.y < transform.position.y - alturaCombate) {
         alturaComOK = false;
     }



     GetComponent<Rigidbody2D>().velocity = new Vector2 (velHorizontal, velVertical);


     // EVALUANDO COMBATE
     if (alturaComOK 
         && leono.transform.position.x >= transform.position.x - rangoCombate
         && leono.transform.position.x <= transform.position.x + rangoCombate
     ) {

         IDLE = false;
         PERSECUCION = false;
         COMBATE = true;
     }



     //EVALUANDO PERSECUCION
     if ((!siendoGolpeado
         && !golpeando)
         && alturaPerOK 
         && leono.transform.position.x >= transform.position.x - rangoPersecucion 
         && leono.transform.position.x < transform.position.x - rangoCombate
         ||
         (!siendoGolpeado
         && !golpeando)
         && alturaPerOK
         && leono.transform.position.x > transform.position.x + rangoCombate
         && leono.transform.position.x <= transform.position.x + rangoPersecucion
     ) {
         
         IDLE = false;
         COMBATE = false;
         PERSECUCION = true;


     }


     //EVALUANDO IDLE
     if (
         !alturaPerOK || 
         leono.transform.position.x < transform.position.x - rangoPersecucion
         || leono.transform.position.x > transform.position.x + rangoPersecucion
         ||
         (leono.transform.position.x >= transform.position.x - rangoCombate
             && leono.transform.position.x <= transform.position.x + rangoCombate && !alturaComOK)
     ) {
         

         PERSECUCION = false;
         COMBATE = false;
         IDLE = true;

     }


     if (COMBATE) {
         Debug.Log ("combate");
         velHorizontal = 0;

         if (leono.animador.GetCurrentAnimatorStateInfo (0).IsTag ("ataque en tierra") 
             && transform.localScale.x != leono.transform.localScale.x) {
             animador.SetTrigger ("reptil 1 herido");
         }

         if (siendoGolpeado) {
             velHorizontal = 0;
             Debug.Log ("siendo golpeado");
         }

         if (golpeando) {
             velHorizontal = 0;
         }

         if (!siendoGolpeado || !golpeando) {
             Debug.Log (" no siendo golpeado");

             animador.SetInteger ("reptil 1", 0);
             tiempoDuranteElCualMePuedenGolpear -= Time.deltaTime;
         }
         if (tiempoDuranteElCualMePuedenGolpear <= 0 ) {
             Debug.Log ("over");
             animador.SetTrigger ("reptil 1 ataque");
             tiempoDuranteElCualMePuedenGolpear = segundosDuranteElCualMePuedenGolpear;
         }

         if (tiempoDuranteElCualMePuedenGolpear <= 0 || golpeando || siendoGolpeado) {
             tiempoDuranteElCualMePuedenGolpear = segundosDuranteElCualMePuedenGolpear;
         }
     }


     if (!COMBATE) {
         tiempoDuranteElCualMePuedenGolpear = segundosDuranteElCualMePuedenGolpear;

         golpeando = false;
     }

     if (siendoGolpeado) {
         velHorizontal = 0;
         Debug.Log ("siendo golpeado");

     }






     if (IDLE) {
         Debug.Log ("idle");
         animador.SetInteger ("reptil 1",0);
         velHorizontal = 0f;
     }




     if (PERSECUCION) {
         Debug.Log ("persecucion");

         if (leono.transform.position.x == transform.position.x) {
             velHorizontal = 0;
             animador.SetInteger ("reptil 1", 0);
         }

         if ( leono.transform.position.x < transform.position.x 
             //- rangoPersecucion 
             //&& leono.transform.position.x < transform.position.x - rangoCombate
         ) {
             transform.localScale = new Vector2 (-1, 1);
                 animador.SetInteger ("reptil 1",1);
             velHorizontal = -velocidadTranco;
             if (siendoGolpeado) {
                 velHorizontal = 0;
             }
         }
         if (leono.transform.position.x > transform.position.x
             //+ rangoCombate
             // && leono.transform.position.x <= transform.position.x + rangoPersecucion
         ) {
             transform.localScale = new Vector2 (1, 1);
                 animador.SetInteger ("reptil 1",1);
             velHorizontal = velocidadTranco;
             if (siendoGolpeado) {
                 velHorizontal = 0;
             }
         }
     }
 }

}

additional note: If I make a duplicate of my player, they all go after the duplicate, even the camera moves towards the duplicate

17-05-2017-14-19-50.png (258.1 kB)
me-pego.png (233.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

354 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Create a menu in script and inspector ?? 2 Answers

How can I give each instance a enum mode from another script ? 0 Answers

Unity freeze when open script,I keep trying to open a script, but it freezes Unity. Please help. 1 Answer

Okay, I am very new to Unity but whenever i try something it called an error on the brackets but there is nothing wrong with them. 1 Answer

How to make object spawn only once? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges