OnTriggerEnter2D Detect Trigger Forever
**Hi! my trouble it's this... When the Object n°1 enter in the Object n°2 (this another with the script with OnTriggerEnter2D function) it should discount points of life, but the life of the enemy (object n°2) decrease life's continously all object have: Rigidbody 2d's , Colliders 2d's and all active "Trigger" why i need.
object N°1 ----> Rayo (Rain / weapon)
object n°2 .-----> Enemigo (Enemy/Prefab)**
Enemy C# code
using UnityEngine;
using System.Collections;
public class EnemigoSalud : MonoBehaviour {
public int vidaMaxima;
public int vidaActual;
public int fuerza;
bool tocado;
void Start () {
vidaMaxima=100;
vidaActual=vidaMaxima;
}
void Update () {
if (tocado){
vidaActual-=fuerza;
}
}
void OnTriggerEnter2D (Collider2D Other){
if (Other.CompareTag("Rayo")){
tocado=true;
}
}
void OnTriggerExit2D (Collider2D Other){
tocado=false;
}
}
Rain C# code
using UnityEngine;
using System.Collections;
public class Rayo : MonoBehaviour {
void Update (){
if (Input.GetMouseButtonUp(0))
Destroy(gameObject);
}
}
Answer by jmonasterio · Dec 12, 2015 at 03:15 PM
Maybe add a line after line 17:
tocado = false;
So you have something like this:
void Update () {
if (tocado){
vidaActual-=fuerza;
tocado = false; // New line.
}
}
That way you only lose one life when the trigger happens. Before you were losing on life on each frame Update().
Thanks you are right, it's a logic error or logic solution, I think that OntriggerEnter2D only ejecute once time, but no.... yesterday i am to tired and not see it, today works fine with the new code line, Thanks agaian!