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 unity_BD862D51689412C149C8 · Feb 25, 2021 at 06:32 PM · physicsplayer

HOW TO MAKE ENEMY SHOOT ONLY PLAYER

Hi l am looking for a way to make enemy only only target and shoot player when player has entered trigger.Here is my script.

 public Rigidbody bulletPrefab;
 public float shootSpeed = 300;

 void OnTriggerEnter(Collider other)
 {
     if(other.tag == "player")
     {
         shootBullet();
     }
 }
 void shootBullet()
 {
    
     var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);

     //Shoot the Bullet in the forward direction of the player
     projectile.velocity = transform.forward * shootSpeed;
 },Hi l am looking for a way to make  my enemy shoot only player when player has entered trigger. Here is my script.

Enemy Script.

 public Rigidbody bulletPrefab;
 public float shootSpeed = 300;

 void OnTriggerEnter(Collider other)
 {
     if(other.tag == "player")
     {
         shootBullet();
     }
 }
 void shootBullet()
 {
    
     var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);

     //Shoot the Bullet in the forward direction of the player
     projectile.velocity = transform.forward * shootSpeed;
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GeroNL · Feb 26, 2021 at 05:49 AM

I will try add what are you need:


Need add collision for trigger enemy shoot, idk you already put or not, just make sure you need 2 collisons in enemy, for enamy can shoot and when enemy get shoot/attacked. the easy one is put in GameObject into child, and give it collsion, and the parent get reference from it.


Need more adjust direction for object like Player / enemy / projectile, if you already adjustment for player and enemy, you can get it direction from it, cause you did not show up the code for it, i just can say that.


Hope it help.

Comment
Add comment · 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
0

Answer by pauldarius98 · Feb 26, 2021 at 07:46 AM

There are a couple of things that you are missing. First, with the current code the enemy will shoot the player only once/trigger enter which might not be the desired behaviour and second, the bullet is fired using the enemy forward direction but the enemy is not rotated towards the player. Try the following code:

      public Rigidbody bulletPrefab;
      public float shootSpeed = 300;
      
      private bool playerInRange = false;
      private float lastAttackTime = 0f;
      private float fireRate = 0.5f; //how many bullets are fired/second
      private Transform player = null;
      
      void OnTriggerEnter(Collider other)
      {
          if(other.tag == "player")
          {
              playerInRange = true;
              player = other.transform;
          }
      }
      
      void OnTriggerExit(Collider other)
      {
          if(other.tag == "player")
          {
              playerInRange = false;
              player = null;
          }
      }
      
      void Update()
      {
          if (playerInRange)
          {
              //Rotate the enemy towards the player
              transform.rotation = Quaternion.LookRotation(player.position - transform.position, transform.up);
              
              if (Time.time - lastAttackTime >= 1f/fireRate)
              {
                  shootBullet();
                  lastAttackTime = Time.time;
              }
          }
      }
      
      void shootBullet()
      {
         
          var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);
          //Shoot the Bullet in the forward direction of the player
          projectile.velocity = transform.forward * shootSpeed;
      }

Comment
Add comment · 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

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

207 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

AddExplosionForce 0 Answers

Keeping player stationary and let physics affect... 0 Answers

Best way to physics collision 1 Answer

How can a player push an object? 5 Answers

Non slippery movement 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