Can't make my AI shoot projectiles with raycast
Hello,
I'm kinda stuck and I need some help in order to make my AI shoot projectiles with the usage of a raycast. The idea is that I want to apply my already existing script for the player (see below) and add it to my AI's shootPlayer function in its own script (see below) but I've never worked with raycasts (nor AIs for that matter) before and I seem at a lose as how to implement my already functional stuff along with an AI and the usage of a raycast.
Player Shooting script: https://gist.github.com/fiskefyren/36781777f24b65a13d39
AI script: https://gist.github.com/fiskefyren/c06ce230837120bad4ea
Thanks in advance :)
You're doing it right. You might want to change the direction of the raycast though. Use (target.position - AI.position).normalized to get a Vector3 direction between the target and the AI, this way the raycast will always be aimed at the player (although this can mean the AI will shoot backwards so you should probably also turn the AI in this direction).
yeah i don't want that... my issues lies more with i'm not sure how i get my AI to even shoot...
Just do the same as with the player. Instantiate your bullet or whatever and move it in a direction and have the collision detection and damage calculation on the bullet. I don't even see why you want to use raycast. Do you want to damage the player directly? If you don't you shouldn't use raycast there (you could use it for detection though), but you don't want the AI to turn and shoot at the player, so I don't know why you would even want that. You just want your AI to shoot aimlessly and hope it hits something it sounds like, so then just loop an enumerator "Shoot()" and use a yield return WaitForSeconds() to deter$$anonymous$$e how often the AI should shoot.
... but the raycast already works as intended... i'm really confused why you keep talking about it... i just can't figure out how to make it work probably when it detects the player (which it does just fine)...
as for SphereCast or SphereOverlap etc. I've no idea what that is... it's not something I've used before... i just wanted to make something simple where the raycast simply shoots the projectile when the raycast hits the player, that was really all... but for some reason I'm having issues making that happen.
i honestly don't understand why you don't understand what i want to do... i've no idea how else to explain it... but maybe your ideas are better? i wouldn't know, and i sure as heck don't know right now how i would go about doing your suggestions...
Again, what are you trying to do? What does "work properly" mean? Apparently it doesn't quite do that since you seem to need help. What isn't working as intended and why? First of, you instantiate your bullets OUTSIDE of the raycast-check. Is this intentional or not? I don't know. If it's not check out the code below. Try this code:
RaycastHit rayHit;
if (Physics.Raycast(transform.position, transform.forward, out rayHit)) { //out is a keyword, you can add another var for the distance of the ray
if(rayHit.transform.gameObject.CompareTag("Player")){ //did we hit the player?
isFiring = true;
GameObject.Instantiate(laserPrefab, leftFirePoint.position, leftFirePoint.rotation);
GameObject.Instantiate(laserPrefab, rightFirePoint.position, rightFirePoint.rotation);
}
/*
I'm clueless D:
*/
}
Debug.Log("Shooting at player!" + rayHit.collider.gameObject.tag);
}
This will only shoot if you hit the GameObject with the tag "Player". And again use a Coroutine (as I linked to previously) for looping the shooting. Use something like: IEnumerator Shoot(){ while(isFiring){ GameObject.Instantiate(bulletPrefab); yield return new WaitForSeconds(cooldown);}}
Your answer
Follow this Question
Related Questions
Shoot towards target 0 Answers
making a boomerang effect in a 2D enviorment 1 Answer
Bullet goes sideways when the player gets hit 1 Answer
Projectile trajectory based on angle 0 Answers