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 El-Deiablo · Apr 21, 2016 at 11:04 PM · spritespriterendererarray of gameobjectsflippingspawn points

Flip sprites in enemy spawner

I am so close to figuring this out. A random running player is selected from the array and placed in a random spawn location (also obtained from an array). If the player spawns on the right he faces and runs to the left side and vice versa. I have gotten my code to do this, but it is inconsistent with the player sometimes spawning in the wrong direction. What is causing this? Thanks in advance!

Here's my code:

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 Awake () 
 {
     mySpriteRenderer = GetComponent<SpriteRenderer>();

 }

 void Start(){
     
     InvokeRepeating ("SpawnPlayers", spawnTime, spawnTime);

     currentSpeed = Random.Range (minSpeed, maxSpeed);

     int spawnIndex = Random.Range (0, spawnPoints.Length);

     foreach (var player in Players) {

         if (spawnPoints [spawnIndex].position.x == 10) {
             player.GetComponent <SpriteRenderer > ().flipX = true;
         }
         else if (spawnPoints [spawnIndex].position.x == -10) {
             player.GetComponent <SpriteRenderer > ().flipX = false;
         }

     }
 }

 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 == 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jgodfrey · Apr 22, 2016 at 01:52 AM

I don't quite follow what you're doing above, but it looks more complex than necessary. Additionally, I wonder if you're problem is related to the fact that you're trying to use an "==" comparison on a float variable (position.x == 10 and postion .x == -10). That's potentially problematic.

By the looks for your code, I guess your spawn points are only 10 or -10 in the x, right? Perhaps replacing this:

      if (spawnPoints [spawnIndex].position.x == 10) {
          player.GetComponent <SpriteRenderer > ().flipX = true;
      }
      else if (spawnPoints [spawnIndex].position.x == -10) {
          player.GetComponent <SpriteRenderer > ().flipX = false;
      }

WIth something like this might work?

 player.GetComponent<SpriteRenderer>().flipX = spawnPoints[spawnIndex].position.x > 0;

That has a few advantages, including:

  • much simpler

  • much safer (no == comparison against a float var)

That will assign flipX = true if the position.X is > 0 (so the "10") and false if the position.x < 0 (so, the "-10"). Also, since there's no "==" check against a float variable, it's a much safer comparison.

It looks like you're doing the same basic foreach loop in both Start() and Update(). Again, I'm not sure what your intent is there, but I assume you can change both to something like the above.

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 El-Deiablo · Apr 22, 2016 at 02:32 PM 0
Share

Thanks for getting back to me! I am still having the same issue after including your code. Some of the players spawn facing the correct side, but others are not. It's very random. I don't understand what the problem is! Does it have something to do with the gameobjects never being destroyed? I was going to add this in later, but maybe this is causing problems. So close!!

Here is my updated code:

public class TempSpawner : $$anonymous$$onoBehaviour{

public Transform []spawnPoints; public float spawnTime = 2f; public GameObject [] Players;

void Start(){

     InvokeRepeating ("SpawnPlayers", spawnTime, spawnTime);
 }

void Update(){

     int spawnIndex = Random.Range (0, spawnPoints.Length);

     foreach (var player in Players) {    

player.GetComponent ().flipX = spawnPoints [spawnIndex].position.x > 0; } }

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 );
 }



}

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

Flipping a group of sprites 1 Answer

Sprite Renderer unlit on devices when using Unity 5.2.0 0 Answers

How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers

Sprite Mask Render Issue 1 Answer

Why do my GameObjects have a flickering line above them? 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