• 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 Internetman · Apr 20, 2016 at 09:25 AM · instantiatearraypositionenemydirection

Make object move in a direction depending on where it spawns? (C#)

Hello! I have some enemies that I would like to "glide" over the screen using transform.translate, the only problem is that I'm not sure how to do this because I spawn out the enemies at a random spawnIndex that is placed on the map.

I'm trying to make it so that when an enemy spawn for example on the top of the screen, it will move downwards on the y-axis, and when the enemy spawns at the bottom it will move upwards on the y-axis.

Should I use a IF statement to check if the enemy spawns at a specific spawnIndex or should I use addforce on the object?

Here's the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EnemySpawn : MonoBehaviour 
 {
     // EnemyPrefabs
     public GameObject EnemyFlyByPrefab;
 
     //Show where spawn is prefab
     public GameObject ShowEnemySpawn;
 
 
     // Array of spawn points 
     public Transform[] spawnPointsEnemyFlyBy;
     
 
     //Used to put active enemies in a list
     public static List<GameObject> activeEnemies;

 
     void Start () 
     {
         // Starts a coroutine function for spawning bouncing enemies and fly by enemy
         StartCoroutine(SpawnEnemyFlyBy());
 
     }
 
     void Awake()
     {
         // Create the list
         activeEnemies = new List<GameObject>();
 
     }
 
     void Update()
     {
 


     IEnumerator SpawnEnemyFlyBy() 
     {
         //Wait 3 to 5 seconds when game starts to spawn the fly by enemy
         yield return new WaitForSeconds(Random.Range(1, 1));
         
         while(true)
         {
             // Find a random index between zero and one less than the number of spawn points.
             int spawnPointIndex = Random.Range (0, spawnPointsEnemyFlyBy.Length);
             
             // Show spawn location for one second
             Object marker = Instantiate(ShowEnemySpawn, spawnPointsEnemyFlyBy[spawnPointIndex].position, Quaternion.identity);
             yield return new WaitForSeconds(1);
             Destroy(marker);
             
             // Spawn enemy
             GameObject newEnemy = (GameObject) Instantiate(EnemyFlyByPrefab, 
                                                            spawnPointsEnemyFlyBy[spawnPointIndex].position, 
                                                            spawnPointsEnemyFlyBy[spawnPointIndex].rotation);

                     // **THIS DOESN'T WORK, HOW CAN I FIX IT?**
             /*if(spawnPointIndex == 1 || spawnPointIndex == 2 || spawnPointIndex == 3)
             {
                 GameObject.Find("Enemy_fly_by").transform.Translate(0, 15, 0);
             }*/
             
             activeEnemies.Add(newEnemy);
             
             yield return new WaitForSeconds(Random.Range(4, 6));
             
         }
         
     }


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 MobinYaqoob · Apr 20, 2016 at 09:36 AM

Use tags. For Example your upper spawn point are tag like "UpperSpawnPoint" and lower are tag like "LowerSpawnPoint"

So just before you instantiate an enemy object where you randomly get the spawn point check what its tag either its UpperSpawnPoint or LowerSpawnPoint so instantiate and on that tag basic make a if check and apply the force Easy :)

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 Internetman · Apr 20, 2016 at 11:44 AM 0
Share

There we go, thank you my friend I got it to work! :D

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

50 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

Related Questions

How do I instantiate same tag/name GameObjects with an array 1 Answer

How to only instantiate objects once? 1 Answer

How can I get a GameObject's transform in game and set that to a variable? 1 Answer

Use GameObject's global position instead of local position 3 Answers

instantiating objects problem 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