Raycast hit doesn't work with tag
Hi guys
I have an object in my game which is tagged as "Enemy" and it's in a layer called "Enemy" as well. It has a rigidbody and a box collider attached to it. The is trigger option is marked. However when I shoot at it, the hit is not recognized.
I have this script attached to the player. The player is in a layer called "Player" and the gun is a child of a camera which is child of the player.
public float nDisparo = 15f;
private float TempoProximoDisparo;
public float damage = 20f;
private Animator ZoomCameraIn;
private bool zoomed;
private Camera maiCam;
private GameObject mira;
// Start is called before the first frame update
void Awake()
{
gerenciaArma = GetComponent<GerenciaArma>();
ZoomCameraIn = transform.Find(Tags.LOOK_ROOT).transform.Find(Tags.ZOOM_CAMERA).GetComponent<Animator>();
mira = GameObject.FindWithTag(Tags.MIRA);
maiCam = Camera.main;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
Atira();
ZoomInAndOut();
}
void Atira()
{
if (Input.GetMouseButtonDown(0))
{
if(gerenciaArma.SelecionaArma().tipoBala == WeaponBulletType.BULLET)
{
gerenciaArma.SelecionaArma().AnimacaoTiro();
DisparaBala();
}
}
}
void ZoomInAndOut()
{
if (gerenciaArma.SelecionaArma().mira_tipo == TipoMira.AIM)
{
if (Input.GetMouseButtonDown(1))
{
ZoomCameraIn.Play(Animacoes.ZOOM_IN_ANIM);
// gerenciaArma.SelecionaArma().Aim(true);
mira.SetActive(false);
print("VaiZoom");
}
if (Input.GetMouseButtonUp(1))//se soltar o botão do mouse
{
ZoomCameraIn.Play(Animacoes.ZOOM_OUT_ANIM);
//gerenciaArma.SelecionaArma().Aim(false);
mira.SetActive(true);
}
}
}
void DisparaBala()
{
RaycastHit hit;
if(Physics.Raycast(maiCam.transform.position, maiCam.transform.forward, out hit))
{
print("We hit: " + hit.transform.gameObject.tag);
if (hit.transform.tag == Tags.ENEMY_TAG)
{
hit.transform.GetComponent<ScriptVida>().DanoAplicado(damage);
}
}
}
}
The hit doesn't recognize the tag. It pass through the enemy directly. What Could I do to solve this issue?
if you remove the tag line it works? add this line and make sure the enemy gets print
Debug.Log(GameObject.FindWithTag(Tags.ENE$$anonymous$$Y_TAG).name);
Your answer
Follow this Question
Related Questions
RayCastHit Not Returning Rigidbody 0 Answers
Advanced AI sight? AI can detect you if part of you is visible to them. Is it Possible? 1 Answer
How to cast a Ray using local pivot of an object? 1 Answer
Why doesnt the value change? 2 Answers
How to instantiate objects to mesh while restricting the spawn area on a single axis? 2 Answers