Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Ayuda con un codigo

Iniciado por kalm97, Enero 30, 2016, 10:22:21 PM

Tema anterior - Siguiente tema
Ayuda tengo estos dos codigos uno es el AI de un enemigo y el otro es el codigo de la bala de mi jugador no se que pasa que cuando la bala entra en el collider del enemigo no le baja vida me podrian ver que pasa....Este es el de la balapublic bool right;
    public bool prueba = false;    public float velocidad = 10;
    public float TimeDestroy = 10;    public int dmg = 25;    private FirePlantAI fire;    void Start()
    {
        fire = GameObject.FindGameObjectWithTag("Enemy").GetComponent<FirePlantAI>();
    }    void Update()
    {
        Destroy(gameObject, TimeDestroy);
        if (prueba)
        {
            fire.prueba = true;
        }        if (right)
        {
            transform.Translate(Vector3.right * Time.deltaTime * velocidad, Camera.main.transform);
        }
        else
        {
            transform.Translate(Vector3.left * Time.deltaTime * velocidad, Camera.main.transform);
        }    }
    void OnTriggerEnter2D(Collider2D col)
    {
        
        if (col.isTrigger != true && col.CompareTag("Enemy"))
        {
            col.SendMessageUpwards("Damage", dmg);
//            Destroy(gameObject);
        }
    }
} y este el del AI 
    public int curHealth;
    public int maxHealth = 100;    public float distance;
    public float wakeRange;
    public float shootInterval;
    public float bullerSpeed = 100;
    public float bulletTimer;    public bool Rise = false;
    public bool Right = true;
    public bool prueba = false;    public GameObject bullet;
    public Transform target;
    public Animator anim;
    public Transform shootPointLeft;
    public Transform shootPointRight;    void Awake()
    {
        anim = gameObject.GetComponent<Animator>();    }
    void Start()
    {
        curHealth = maxHealth;
    }
    void Update()
    {
        if (curHealth <= 0)
        {
            Die();
        }
        if (curHealth > maxHealth)
        {
            curHealth = maxHealth;
        }
        anim.SetBool("Rise", Rise);
        anim.SetBool("Right", Right);        RangeCheck();
        if (target.transform.position.x > transform.position.x)
        {
            Right = true;
        }
        if (target.transform.position.x < transform.position.x)
        {
            Right = false;
        }
    }
    void RangeCheck()
    {
        distance = Vector3.Distance(transform.position, target.transform.position);
        if (distance < wakeRange)
        {
            Rise = true;
        }
        if(distance > wakeRange)
        {
            Rise = false;
        }
    }    public void Attack(bool attackingRight)
    {
        bulletTimer += Time.deltaTime;        if (bulletTimer >= shootInterval)
        {
            Vector2 direction = target.transform.position - transform.position;
            direction.Normalize();            if (!attackingRight)
            {
                GameObject bulletClone;
                bulletClone = Instantiate(bullet, shootPointLeft.transform.position, shootPointLeft.transform.rotation) as GameObject;
                bulletClone.GetComponent<Rigidbody2D>().velocity = direction * bullerSpeed;                bulletTimer = 0;
            }
            if (attackingRight)
            {
                GameObject bulletClone;
                bulletClone = Instantiate(bullet, shootPointRight.transform.position, shootPointRight.transform.rotation) as GameObject;
                bulletClone.GetComponent<Rigidbody2D>().velocity = direction * bullerSpeed;                bulletTimer = 0;
            }
        }
    }
    public void Damage(int damage)
    {
        curHealth -= damage;
        gameObject.GetComponent<Animation>().Play("Player_Damage");    }
    void Die()
    {
        Destroy(gameObject);
    } Grasias por su atencion.. 

Prueba a obtener el componente 'FirePlantAI' en el momento de la colisión de la bala, y llama a la función 'Damage' desde ahí.
if(col.transform.tag == "Enemy")
{
    fire = col.GetComponent<FirePlantAI>();
    fire.Damage(dmg);
}
 

aun no le reconoce le disparo y no sucede nada  

Asegúrate de que el enemigo tenga el tag Enemy y pon un debug dentro del if para saber si por lo menos detecta la colisión con el enemigo. 'Debug.Log("contacto" + col.name);'

No entiendo que pasa ya verifique el enemigo tiene el tag ya intente que colisione con el personaje y si funciona y le puse el tag al personaje pero tampoco funciona es como si el tag no tubiera valor para el codigo como si no existiera

Ya me funciono, le puse un rigybody y funciono.. alguien me dise porque funciono..???que no tengo idea.....!!!

por que para que funcone los metodos de colision , es necesario que almenos uno de los objetos a colisionar tenga rigidbody, ya sea el enemigo o la bala, alfin de cuentas la logica indica que una colision implica un impacto, el impacto implica un cuerpo y el cuerpo tiene masa y el componente con masa es el rigidbody,  jaja la verdad es lo que yo pienso pero de que se ocupa se ocupa, deja investigo en la documentacion  por que ocupa un rigidybody jajaj para darte una opinion mas profesional n_n 

Etiquetas: