• 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
0

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

bullets are spawning under me and they don't move 1 Answer

Random Raycast inside crosshairs 1 Answer

How do I stop a 2D projectile when it reaches a point 3 Answers

How do I make enemies only follow my player character when they are close enough to him?,How do I make it so that an enemy only follows my player when the player character is close enough to the enemy. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges