0
votes

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?

1
After you instantiated your GameObject you can reference it's transform and then call the LookAt method on it. Example: tiro.transform.LookAt(targetPosition)lassedev
@lassedev I've tried this: tiro.transform.LookAt(hit.transform.position), but it didn't work. The player doesn't rotate either.user11660804

1 Answers

0
votes

what you would need to do is add the LookAt inside the script controlling the projectile motion, you must have a line of code that overrides your tiro.transform.LookAt(hit.transform.position). Another thing I am not sure of is are you using the previous line of code in the update function (so it looks at the target permanently) or just in the shoot function (so it looks at the target on its first frame)?

PS: Although this is not a final answer, it might help you and I would have written it in the comment section but I do not have enough rights.