Problem with Enemy AI
This question will probably solve itself as i search through out the internet but ill give it another shot.
So this time, my problem revolves around a Entity that i want to make in my game. This Entity, the Enemy, has basically basic functions on following the target but a bit lacking on its shooting part. It basically is going to follow its target until it goes close enough to it so it shoots. Half the part is done, with the enemy stalking the player until its in range. The problem i am having right now is that when it shoots it does the following:
It doesn't shoot straight.
Its fire-rate is really high. Even with the limitation on the script. (I probably have screwed something up)
(Here is what i deal with.. https://gyazo.com/0ff257d8260c74340fbbce6919188244)
I am not really sure how to solve this considering i am relatively new to scripting in C# but i would like to have some insight on this.
C# Code : using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
public float playerTargetDistance;
public float shootingRange;
public float lifetime = 5f;
public float speed = 20f;
public float baseSpeed = 20f;
public Transform playerTarget;
public Rigidbody EnemyBullet;
public Transform EnemyChamber;
public float shotTime;
public float intervalShots = 1.7f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (shotTime > 0) {
shotTime -= Time.deltaTime;
}
if(Vector3.Distance(transform.position, playerTarget.position) <= playerTargetDistance){
transform.position = Vector3.MoveTowards(transform.position, playerTarget.position, speed);
transform.LookAt(playerTarget);
if(Vector3.Distance(transform.position, playerTarget.position) <= shootingRange){
speed = 0;
Shoot();
}
else
{
speed = baseSpeed;
}
}
}
void Shoot() {
Rigidbody EnemyShot;
EnemyShot = Instantiate (EnemyBullet, EnemyChamber.position, EnemyChamber.rotation) as Rigidbody;
EnemyShot.AddForce (EnemyChamber.forward * 5000f);
Destroy (EnemyShot.gameObject,lifetime);
}
}
P.S: I might find the solution on the internet but at this rate, i wont have any luck finding the right post for it. So any helpful sites will help me a hole lot! :D
In terms of the fire rate, you don't actually have anything limiting it unless I'm missing it. You have the if(shotTime) check, but that's not tied to anything else, so it's just making that comparison and then doing nothing with it. So you'll need to incorporate that into your check for if shooting is valid or not.
For it not shooting straight, I guess you mean it's going sideways? I would add force to the EnemyShot's forward, not the chamber's. Just make sure your EnemyShot's forward is the right direction when it spawns, which it looks like you probably have.
The script that i compiled is basicly what i used on my player. Basically what i mean is that the shotTimer that im using right now is being used the same way as the player and it works there.
But i might now know the mistake and since this problem was from like weeks ago, im gonna remake the scripts i had so far.
Answer by Wizardwolfje · Mar 29, 2016 at 07:28 PM
Try this script it works for me but it doesnt shoot but only follow using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { Transform tr_Player; float f_RotSpeed=3.0f,f_MoveSpeed = 3.0f;
// Use this for initialization
void Start () {
tr_Player = GameObject.FindGameObjectWithTag ("Player").transform; }
// Update is called once per frame
void Update () {
/* Look at Player*/
transform.rotation = Quaternion.Slerp (transform.rotation , Quaternion.LookRotation (tr_Player.position - transform.position) , f_RotSpeed * Time.deltaTime);
/* Move at Player*/
transform.position += transform.forward * f_MoveSpeed * Time.deltaTime;
}
}
Ill try and tweek the script to my liking. Ill reply you with my script when im done. :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612073743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Score counter not working properly. 1 Answer
Vector3.Lerp not working properly, making the player bounce around 2 Answers
arrow from text to object - complete beginner 0 Answers
videoPlayer Script Help Needed 0 Answers
The laser problems 0 Answers