Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 delfinatarga · Nov 01, 2017 at 01:23 AM · enemyattack

Enemy attack when it spawns

Hello! Sorry if this is a dumb question but i'm new to unity. Right now i have a script that makes the enemies spawn in four positions with a delay, but now i need the enemy to attack the player a couple seconds after it spawns. I'm having trouble doing that, but also when the enemy attacks an image should appear on the screen to make the player realize it has been attack, and i don't know how to do that without a bullet or something that collides with the player. Here's my spawn script

 public class TimedSpawn : MonoBehaviour {
 
     public GameObject spawnee;
     public bool stopSpawning;
     public float spawnTime;
     public float spawnDelay;
 
     // Use this for initialization
     void Start () {
         InvokeRepeating ("SpawnObject", spawnTime, spawnDelay);
     }
 
     public void SpawnObject (){
         Instantiate (spawnee, transform.position, transform.rotation);
 
         if (stopSpawning) {
             CancelInvoke ("SpawnObject");
         }
     }
 }
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 AidanHorton · Nov 01, 2017 at 11:58 AM

What you should do to attack the player after a certain amount of time is start an 'IEnumerator', which allows you to use 'WaitForSeconds'. Call the 'IEnumerator' function after the enemy has spawned, and pass a reference of the enemy to the function. After a few seconds, call the 'enemy.attack' command (or whatever your attack command is).

 public class TimedSpawn : MonoBehaviour {
  
      public GameObject spawnee;
      public bool stopSpawning;
      public float spawnTime;
      public float spawnDelay;
  
      // Use this for initialization
      void Start () {
          InvokeRepeating ("SpawnObject", spawnTime, spawnDelay);
      }
  
      public void SpawnObject (){
          GameObject enemy = (GameObject)Instantiate (spawnee, transform.position, transform.rotation);

          //Call the IEnumerator function, and pass it 'enemy'
          StartCoroutine(Attack(enemy));
  
          if (stopSpawning) {
              CancelInvoke ("SpawnObject");
          }
      }
     
     IEnumerator Attack(GameObject enemy) {
         //Wait for 5 seconds
         yield return new WaitForSeconds(5.0f);
 
         //Attack here! - Something like 'enemy.GetComponent<enemy_script>().Attack();'
     }
  }

As for your other issue you should look into UI (to display the image): https://docs.unity3d.com/Manual/UISystem.html, and Rigidbody collisions for when the bullet hits the player: https://docs.unity3d.com/ScriptReference/Rigidbody.OnCollisionEnter.html

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

72 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

Related Questions

how do I edit enemy stats 3 Answers

how to do to in second tap on screen of smartphone to create object and to this object look toward enemy but object look toward vector3 (0,0,0) 0 Answers

Enemy follows player then attacks! 2 Answers

Damaging enemies... 1 Answer

Get First Object In List 2 Answers


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