- Home /
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;
}
Answer by robertbu · Mar 18, 2013 at 05:16 AM
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?
Ok heres my modified code it works but the shotDelay does not can you help. Thanks in advance. (changing the execution works but then the shotDelay stops after one execution) 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() {
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;
}
Inside Update() make shooting conditional:
if (!shooting)
shootStuff();
Your answer
Follow this Question
Related Questions
How to make the enemy move back to its waypoint after it's target killed? 2 Answers
Clones of enemies will not shoot 0 Answers
get closest enemy 1 Answer
I've got a coding problem please help 1 Answer
AI getting stuck on corners 1 Answer