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

}

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.