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 /
  • Help Room /
avatar image
0
Question by andyipod1437 · Feb 27, 2016 at 03:00 AM · 2dscript.prefabsspawning

Help with spawning Prefabs that go towards a player

alt text I need some writing a script that would help me spawn prefabs that move towards a player.

i have already look at http://answers.unity3d.com/questions/179605/moving-an-object-towards-another-moving-object.html and http://answers.unity3d.com/questions/560190/make-item-spawn-at-a-empty-game-object-location.html

and they dont seem to help me

spawn.jpg (42.9 kB)
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
2

Answer by Zoogyburger · Feb 27, 2016 at 04:57 AM

Here's a spawner script:

 {
     public float spawnTime = 5f;
     //The amount of time between each spawn.
     public float spawnDelay = 3f;
     //The amount of time before spawning starts.
     public GameObject[] enemies;
     //Array of enemy prefabs.
     public Vector3 enposition;
     void Start ()
     {
         //Start calling the Spawn function repeatedly after a delay.
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     void Spawn ()
     {
         //Instantiate a random enemy.
         int enemyIndex = Random.Range(0, enemies.Length);
         Instantiate(enemies[enemyIndex], enposition, transform.rotation);
     }


Here's a enemy move toward player script

 public Transform Player;
 public float speed = 2f;
 private float minDistance = 0.2f;
 private float range;
 void Update ()
 {
     Player = GameObject.FindWithTag ("Player").transform;
     range = Vector2.Distance(transform.position, Player.position);

     if (range > minDistance)
     {
         Debug.Log(range);
         Player = GameObject.FindWithTag ("Player").transform;
         transform.position = Vector2.MoveTowards(transform.position, Player.position, speed * Time.deltaTime);
     }
 }


What you do is attach the spawner script on an empty gameobject at the position you want the enemy to spawn and drag your prefab (with the enemy move toward player script on it) into the slot in the inspector on the spawner.

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 andyipod1437 · Feb 27, 2016 at 02:06 PM

is there any way i get the enemy to spawn the all over the out side and come towards the Player. and When i ran your srcipt it only spawn the enemy once . Thanks @Zoogyburger

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 Zoogyburger · Feb 27, 2016 at 05:38 PM 0
Share

Just duplicate the empty gameobject that has the spawner script on it and move them to the position that you want the enemies to spawn from. If you attach my second script to your enemy prefab they will move toward the player. Inside the inspector on the spanwer you can see two variables, spawnTime and spawnDelay. If you make those variables low, the enemies will spawn faster.

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

77 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

Related Questions

Would it be possible and is it a good idea? [Loading prefabs by name] 0 Answers

Random Layout of tiles notworking in 2d game 1 Answer

Advise on scripting text adventure 1 Answer

How to make objects spawned from 2 players go in different directions. 0 Answers

Documentation about tiles 0 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