Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SebastianTarrant · Oct 10, 2020 at 03:04 PM · 2d gameshootingbulletenemy aifire rate

Trying to make an enemy fire at the player

I'm trying to make the enemy in my game (Simple 2d top-down game) shoot at the player, but so far nothing I've tried has worked. I want the Enemy to shoot with a fire rate of 2 seconds and to only shoot if the player is 10m away(Yes, I realize I don't have any code about the distance but I haven't found anything that says about it so...). Any help would be appreciated.

Here is my script for the Enemy shooting:

public class EnemyFire : MonoBehaviour { float fireRate; float nextFire;

 public Transform FirePoint;
 public GameObject bulletPrefab;

 public float bulletForce = 20f;

 // Start is called before the first frame update
 void Start()
 {
     fireRate = 3f;
     nextFire = Time.fixedDeltaTime;
 }

 // Update is called once per frame
 void Update()
 {
     CheckIfTimeToFire();
 }

 void CheckIfTimeToFire()
 {
     if(Time.fixedDeltaTime > nextFire)
     {
         GameObject Bullet = Instantiate(bulletPrefab, FirePoint.position, FirePoint.rotation);
         Rigidbody2D rb = Bullet.GetComponent<Rigidbody2D>();
         rb.AddForce(FirePoint.up * bulletForce, ForceMode2D.Impulse);
         nextFire = Time.fixedDeltaTime + fireRate;
     }
 }

}

I also have this script set up for the Player shooting, if it helps:

public class Shooting : MonoBehaviour { public Transform FirePoint; public GameObject bulletPrefab;

 public float bulletForce = 20f;

 void Update()
 {
    if (Input.GetButtonDown("Fire1"))
     {
         Shoot();
     }
     
 }

 void Shoot()
 {
     GameObject bullet = Instantiate(bulletPrefab, FirePoint.position, FirePoint.rotation);
     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(FirePoint.up * bulletForce, ForceMode2D.Impulse);
 }
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by VARUN88 · Oct 14, 2020 at 01:43 PM

Hi @SebastianTarrant You can use this for checking the distance

void Update() {

//Check the distance between player and enemy

If(Vector2.Distance(Player.transform.position,transform.position) <=10) bool canShoot = true;

if(time.fixedDeltatime >= nextTimeTofire && canShoot == true) Shoot(); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alex_SSS · Oct 14, 2020 at 03:37 PM 0
Share

Also, i think will be good like this(I'm new at learning Unity, so idk if it will work how i think): //Check the distance between player and enemy

If(Vector2.Distance(Player.transform.position,transform.position) <=10) bool canShoot = true;

else bool canShoot == false;

if(time.fixedDeltatime >= nextTimeTofire && canShoot == true) Shoot(); }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

181 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers

How to change the fire rate on my shooting script? 1 Answer

Shooting a bullet object toward the mouse position in a 2D. 1 Answer

Why will the bullets sometimes not show up in the game view when the player shoots? 0 Answers

How to get an enemy to go to a location and then return.,Hi, 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges