Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Problema na colisão com bala e chefe

Estou tendo problemas ao fazer a colisão da bala com o chefe. O curioso é que se eu fizer a validação da colisão dentro do chefe, funciona. Os zumbis normais também estão funcionando normalmente. Código do chefe:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;    //Biblioteca responsavel pela IA

public class ControlaChefe : MonoBehaviour, IMatavel
{
    private Transform jogador;
    private NavMeshAgent agente;
    private StatusInimigo statusChefe;
    private AnimacaoPersonagem animacaoChefe;
    private MovimentoPersonagem movimentoChefe;

    private void Start()
    {
        jogador = GameObject.FindWithTag("Jogador").transform;

        agente = GetComponent<NavMeshAgent>();

        statusChefe = GetComponent<StatusInimigo>();

        agente.speed = statusChefe.Velocidade;

        animacaoChefe = GetComponent<AnimacaoPersonagem>();

        movimentoChefe = GetComponent<MovimentoPersonagem>();
    }

    private void Update()
    {
        agente.SetDestination(jogador.position);

        animacaoChefe.Movimentar(agente.velocity.magnitude);

        if (agente.hasPath == true)
        {
            bool estouPertoDoJogador = agente.remainingDistance <= agente.stoppingDistance;

            if (estouPertoDoJogador)
            {
                animacaoChefe.Atacar(true);

                Vector3 direcao = jogador.position - transform.position;

                movimentoChefe.Rotacionar(direcao);
            }
            else
            {
                animacaoChefe.Atacar(false);
            }
        }
    }

    void AtacaJogador()
    {
        int dano = Random.Range(30, 40);

        jogador.GetComponent<ControlaJogador>().TomarDano(dano);
    }

    public void TomarDano(int dano)
    {
        statusChefe.Vida -= dano;

        if(statusChefe.Vida <= 0)
        {
            Morrer();
        }
    }

    public void Morrer()
    {
        animacaoChefe.Morrer();

        movimentoChefe.Morrer();

        this.enabled = false;

        agente.enabled = false;

        Destroy(gameObject, 2);
    }

//Colisao da bala com o chefe
    private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Bala")
        {
            TomarDano(1);
        }
    }
}

código da bala:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bala : MonoBehaviour {

    public float Velocidade = 20;
    private Rigidbody rigidbodyBala;
    public AudioClip SomDeMorte;

    private void Start()
    {
        rigidbodyBala = GetComponent<Rigidbody>();
    }

    void FixedUpdate () {

        rigidbodyBala.MovePosition
            (rigidbodyBala.position + 
            transform.forward * Velocidade * Time.deltaTime);
    }

    void OnTriggerEnter(Collider objetoDeColisao)
    {
    //Colisão com if~else
        if (objetoDeColisao.tag == "Inimigo")
        {
            //Tira 1 de dano.
            objetoDeColisao.GetComponent<ControlaInimigo>().TomarDano(1);
        } else if(objetoDeColisao.tag == "Chefe")
        {
            objetoDeColisao.GetComponent<ControlaChefe>().TomarDano(1);
        }

        /*
        //Colisão com switch
        switch (objetoDeColisao.tag)
        {
            case "Inimigo":
                objetoDeColisao.GetComponent<ControlaInimigo>().TomarDano(1);
            break;

            case "Chefe":
                objetoDeColisao.GetComponent<ControlaChefe>().TomarDano(1);
            break;

        }
        */

        //Destroi a bala.
        Destroy(gameObject);

A tag do chefe é realmente "Chefe". Estou usando a versão 2018.3.2f1

1 resposta
solução!

Resolvi o problema, eu tinha dois scripts para o mesmo objeto, um chamado "Bala" e o outro "ControlaBala". O que estava no objeto era o "ControlaBala" e eu estava fazendo as alterações no script "Bala".

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software