I'd like to know how could I use the function Lookat to instantiate the bullet in the opponent's direction? Because in the way that I've programmed, the projectile just goes in a straight line.
I'm using this function to shoot the projectile:
public void TeslaShooting() //Tesla's shooting method
{
for (int i = 0; i < Personagens.Length; i++) {
if (Personagens[i].tag == "Tesla" && Input.GetMouseButtonDown(1) && Personagens[i].GetComponent < Jogador > () && Personagens[i].GetComponent < Jogador > ().PodeAtacar == false && Personagens[i].GetComponent < Jogador > ().isPlayer) {
//raycast checks and returns an object, if it's a tile, the unit shoots
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.tag == "Monster" || hit.transform.tag == "Orc" || hit.transform.tag == "Vampire") {
//instantiates the bullet
GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
Rigidbody BalaRigid = tiro.GetComponent < Rigidbody > ();
BalaRigid.velocity = SpawBala.transform.forward * ProjetilVeloc * Time.deltaTime;
gameManager.PontoAcaop1--;
Personagens[i].GetComponent < Jogador > ().PodeAtacar = true;
interfaceManager.ActionPointDown();
}
}
}
}
}
If the opponent is on my side or in a diagonal position I can't hit him.
How could I solve it?