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 El-Deiablo · May 05, 2016 at 06:01 PM · spritespawningspawnpointsflipping

Enemy Spawner

My game is 2D. I have my game currently set up with several different spawners. I have a left side spawner, right side spawner, and bottom spawner. There are 3 different spawn points in each spawner for a total of 9 spawn points. While this is working it is not what I want exactly. I wanted to have all of my spawn points in 1 array and spawn an enemy in a randomly chosen position from the 9 spawn points. I am unable to do this because my sprites are facing right. I need to flip the sprite depending on which spawn point is selected. If it is on the right side (if(spawnpoint[spawnindex].position.x>0)) flip the sprite. I have tried many things, but nothing seems to work. I would also need to rotate a 2D sprite 90 degrees when it is instantiated at one of the bottom spawn points. I think it may have something to do with where I am calling the arguments, but I have also messed around with that and have had no luck. I would really appreciate some help or guidance.

Here is my code (with several unsuccessful attempts):

using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class TempSpawner : MonoBehaviour{
 
     //public float runningPlayerSpeed;
     public Transform []spawnPoints;
     public float spawnTime = 2f;
     public GameObject [] Players;
 
 
     //public SpriteRenderer mySpriteRenderer;
 
     //public float minSpeed;
     //public float maxSpeed;
     //private float currentSpeed;
 
 
 
     void Start(){
 
 
         InvokeRepeating ("SpawnPlayers", spawnTime, spawnTime);
 
         //currentSpeed = Random.Range (minSpeed, maxSpeed);
     }
 
 
     
 
     void Update(){
         //float amountToMove = currentSpeed * Time.deltaTime;
         //transform.Translate (Vector3.right  * amountToMove);
 
 
         //int spawnIndex = Random.Range (0, spawnPoints.Length);
 
 
         //int objectIndex = Random.Range (0, Players.Length);
 
     
 
         //foreach (var player in Players) {
 
 
             /*if (spawnPoints [spawnIndex].position .x > 0) {
                 tempPos = transform.position;
 
                 tempPos.x = -0.05f;
 
                 transform.position = tempPos;
             }
 
             if (spawnPoints [spawnIndex].position .x < 0) {
                 tempPos = transform.position;
 
                 tempPos.x = 0.05f;
 
                 transform.position = tempPos;
             }*/
             //player.GetComponent <SpriteRenderer > ().flipX = spawnPoints [spawnIndex].position.x > 0;
              
 
             /*if (spawnPoints [spawnIndex].position.x < 0) {
                 player.GetComponent <SpriteRenderer > ().flipX = true;
             }
             if (spawnPoints [spawnIndex].position.x > 0) {
                 player.GetComponent <SpriteRenderer > ().flipX = false;
             }*/
 
 
         }
             /*if (spawnPoints [spawnIndex].position.x == 10) {
                 player.GetComponent <SpriteRenderer > ().flipX = true;
 
                 //SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();
                 //sr.flipX = true;
 
 
 
                 //Players [objectIndex].GetComponent <SpriteRenderer   > (mySpriteRenderer );
                 //mySpriteRenderer.flipX = true;
 
                 //Players [objectIndex].transform.localScale.x  == -1f;
                 //mySpriteRenderer.flipX = true;
                 //GameObject.FindGameObjectsWithTag ("Running Players");
 
     
             } else if (spawnPoints [spawnIndex].position.x == -10) {
                 player.GetComponent <SpriteRenderer > ().flipX = false;
             }
     }*/
 
 
     void SpawnPlayers(){
         int spawnIndex = Random.Range (0, spawnPoints.Length);
         int objectIndex = Random.Range (0, Players.Length);
         Instantiate (Players [objectIndex], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
     }
 
 
 
 }
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 FortisVenaliter · May 05, 2016 at 09:25 PM

Instead of just storing the spawn points as transforms, why don't you add a new SpawnPoint behaviour that has a float Rotation and a boolean Flip. Then, store them as an array of spawn points, and you can check the randomly selected spawn point to know if the sprite needs to be flipped or rotated.

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 Ryanless · May 05, 2016 at 11:06 PM 0
Share

doesnt /shoundt be a bonohevour but just a simple class

avatar image
0

Answer by Ryanless · May 05, 2016 at 11:12 PM

either do fortis route, or you try a switch as it seems to be easier in this case. So you spawn it at random point and store the index of the point in the array.

and then have switch:

 int index // index of array at which point it spawned
 switch (index){
 case index < 3:
 //code to make it face the right direction;
 
 case index < 6:
 //code to make it face another direction;
 
 default:
 // code to make it face another direct;
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

localScale.x Affecting Player Translations 0 Answers

Random.Onunitsphere confusing different spheres 1 Answer

Spawn an object when player is near and presses button 1 Answer

Can see objects from spawn points in scene view, but not in game. 0 Answers

Scene back and forth Transition Help 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