Raycast Enemy AI shooting script
Okay, so these are the stats. I'm trying to make a third person shooter. I have created two prefab bullets. One is shot by my player. Another is used by my enemies. The idea is for them (the enemies) to move around (chase after me), and shoot at me. I don't want them to fill me with bullets right when the game starts and to continue firing without pause. There should be a fire rate limiting that. I had a script that worked badly. Now I have another one but the enemies just stand around doing nothing at all. Can you please help me to fix it? Thanks! Here's the script:
var fwd : Vector3; //raycast
var distance : float = 10.0f; //distance
var timer : float;
var amountOfBullets : int = 0;
var amountOfBullets_reset;
var bullets_Array : GameObject[];
var hit : UnityEngine.Collision;
function Start() {
fwd = transform.TransformDirection(Vector3.forward);
}
function amountofBullets_reset(){
bullets_Array = GameObject.FindGameObjectsWithTag("Bullet");
amountOfBullets = bullets_Array.Length;
}
function Update ()
{
if (Physics.Raycast (transform.position, fwd, distance)) { //raycast forward and see...
if(hit.transform.gameObject.name == "MainCharacter"){ //whether it is a player...
Fire(); //if this runs, then the player is in the sight of the enemy!
}
}
}
function Fire () {
//inside of firing function:
if(timer > Random.Range(3,5)&&amountOfBullets<5){
Fire();
timer = 0.0f;
}
timer += 1 * Time.deltaTime;
amountOfBullets_reset();
}
@ZefanS @LucasMars You guys have been very wise and helped me greatly in the past. Do you think you could please help me now too? Thanks!
Are the bullets being destroyed when entering the player? If not, then the bullets are just increasing in numbers, and when it gets to 5, it cannot create any more. I know you have a script to create blood when the player is hit, but does that blood have a tag of "Bullet"? If so, remove that as the tag.
That should fix it. If it doesn't, comment back and I'll see what else the bug could be.
Answer by Rob2309 · Mar 25, 2016 at 02:43 AM
Your problem might be in your fwd vector: you just set it once at startup, which will initialize it to a forward vector at that point in time... instead of using your fwd vector, you might just use transform.forward which will always point to the direction your transform is looking at... :)
That makes sense as to be the answer. Alternatively from using
transform.forward
, you could initialize the fwd variable inside of update.
@Rob2309 @Lucas$$anonymous$$ars @Zefan S
Thank you so much for your input. I have tried this solution but I kept getting other play time errors so I've abandoned that script. I've found another one. This one has no errors but the bullets instantiate too low, the fire rate this too high (constant barrage of bullets) and the characters move while floating around ins$$anonymous$$d of walking (I'm currently using the standard AI Third Person Controller. So, please if you could assist me in fixing it, it would be great: This is the script:
public var Target : Transform;
public var Projectile : Transform;
public var $$anonymous$$aximumLookDistance : float = 15;
public var $$anonymous$$aximumAttackDistance : float = 10;
public var FollowSpeed : float = 5;
public var $$anonymous$$inimumDistanceFromPlayer : float = 2;
public var RotationDamping : float = 2;
public var $$anonymous$$oveSpeed : float = 1;
function Update () {
var distance = Vector3.Distance(Target.position, transform.position);
if(distance <= $$anonymous$$aximumLookDistance) {
LookAtTarget ();
if(distance <= $$anonymous$$aximumAttackDistance)
AttackAndFollowTarget (distance);
}
}
function LookAtTarget () {
var dir = Target.position - transform.position;
dir.y = 0;
var rotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * RotationDamping);
}
function AttackAndFollowTarget (distance : float) {
if(distance > $$anonymous$$inimumDistanceFromPlayer)
transform.Translate((Target.position - transform.position).normalized * $$anonymous$$oveSpeed * Time.deltaTime);
Instantiate(Projectile, transform.position + (Target.position - transform.position).normalized, Quaternion.LookRotation(Target.position - transform.position));
}
For your new script, to fix the bullet being instantiated too low, you do this: var position : Vector3 = transform.position + (Target.position - transform.position).normalized; position.y += 1; //change this to change the height
To make the amount of bullets slow down, you can add an yield WaitForSeconds (time);
Not sure what you mean by the character floating.
For this point: "bullets instantiate too low", you'll need to look into 'Object Pooling'
I've a custom script that I can adapt when I get the chance to do so, that may give you some ideas, if nobody else has posted it by then.
Your answer
Follow this Question
Related Questions
Interesting Enemy AI issue 2 Answers
Enemy AI for Shooting Game 1 Answer
Need help with physics base enemy AI movement 1 Answer
AI Problem. 0 Answers
Patrolling enemy not shooting at player 2d platformer 0 Answers