Hello everyone, I want the enemy to not look at the enemy while shooting here is my code (code works):

var player : Transform;
var safeDist : float = 15;
var currentDist : float;
var shooting : boolean = false;

var bulletFab : Rigidbody;
var gunTip : Transform;
var power : float = 1000;
var shotDelay : float = 1;

function Update () {

currentDist = Vector3.Distance(transform.position, player.position);

if(currentDist < safeDist){
transform.LookAt(player);
if(!shooting) shootStuff();

}

}

function shootStuff(){

shooting = true;





var fwd = transform.TransformDirection(Vector3.forward);
var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
bulletShot.AddForce(fwd * power);

yield WaitForSeconds(shotDelay);
shooting = false;

}

whats the question?

1 Answer

1

I think what you are asking for is the code below, but the behavior may be strange. That is when the shooting stops after the ‘shotDelay’ the game object will immediately ‘jerk’ to the new position if the player has moved. If this is a spawn point, it won’t matter, but if it is a visible object…

if(currentDist < safeDist){
    if(!shooting) {
        transform.LookAt(player);
        shootStuff();
    }
}

Ok thanks everyone for the code, but what I am asking for is for the enemy to not look at the player while shooting, to just shoot basically, with out any sense of direction.

This code stops the LookAt() while shooting, so your player can move and the shots will still be at the position when the firing started. If don't want him to turn at all, then remove the LookAt(). If you want him to start shooting in random directions, then that is a whole other question and needs a lot of clarification about the limits of the randomness. Can he shoot anywhere or still in the general direction of the target for example? How accurate do you want him to be? Do you want the randomness to track the player or only where he was when the shooting started?

Inside Update() make shooting conditional: if (!shooting) shootStuff();

You sir are a genius ;) Thanks A lot!!!!!.