Strange bug in a Space Invaders type game
Hi,i am new to unity and i am trying to create a game like Space Invaders, but i have a bug in the movement script of the invaders. in the Spawn script that is attached to an empty gameobject(scene controller), i create 40 invaders by instantiate the movement serializedfield and i fix their position, then i memorize all the invaders in a list; Once done with that i pass this gameobject in the movement script (that is attached to each of invaders) as serializedfield so i can retieve the list with all the invaders;here first i create a function to controll the object with max pos and with less pos, so i find the invader of the right border and of the left border; in the update method i move the invader since the invader of right border has an position <18, when is>18 i change the speed to negative, and the same for the other side. the problem is that when i run the game only the row with the invader of right border and left border acts in a strange way, when the right border arrives at 18 and changes his speed to negative , all the invaders of the row change their position a little to left and same happens for the other side;
as you can see only the row with the two invaders that i use for changing velocity after a few time shifts without a reason,this movement script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class InvaderMovement : MonoBehaviour
{
[SerializeField]private Spawner a;
public float MinX = -18;
public float MaxX = 18;
public float speed=90;
private int b = 1;
private int indexMAX;
private int indexMIN;
private bool Dir = false;
void Start()
{
speed = -3;
confrontall();
Debug.Log(indexMAX);
Debug.Log(indexMIN);
Debug.Log(a.AllInvaders[indexMAX].transform.position.x);
}
void Update()
{
transform.Translate(new Vector3(speed, 0, 0));
if (a.AllInvaders[indexMAX].transform.position.x >= 18)
{
speed = -speed;
Debug.Log(speed);
Debug.Log("cio");
Dir = true;
}
if (a.AllInvaders[indexMIN].transform.position.x <= -18 )
{
Debug.Log(speed);
speed = Mathf.Abs(speed);
Dir = false;
}
}
public void confrontall()
{
float max = -1000;
float min = 1000;
for(int j=0; j<a.AllInvaders.Count;j++)
{
if (a.AllInvaders[j] != null)
{
if (a.AllInvaders[j].transform.position.x > max)
max = a.AllInvaders[j].transform.position.x;
if (a.AllInvaders[j].transform.position.x < min)
min = a.AllInvaders[j].transform.position.x;
}
}
for (int i=0; i<a.AllInvaders.Count;i++)
{
if (a.AllInvaders[i] != null)
{
if (max == a.AllInvaders[i].transform.position.x)
indexMAX = i;
if (min == a.AllInvaders[i].transform.position.x)
indexMIN = i;
}
}
}
}
this is the Spawn script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField] private InvaderMovement invader;
private InvaderMovement obj;
internal List <InvaderMovement> AllInvaders = new List<InvaderMovement>();
private float offsetX = 3;
private float offsetY = 2;
private int k = 0;
void Awake()
{
int max = 5;
Vector3 OriginalPos = invader.transform.position;
for (int i = 0; i < 4; i++)
{
max = 10;
OriginalPos = invader.transform.position;
OriginalPos.x = OriginalPos.x - offsetX;
for (int j = 0; j < max; j++)
{
AllInvaders.Add(Instantiate(invader) as InvaderMovement);
AllInvaders[k].transform.position = new Vector3(OriginalPos.x + offsetX, OriginalPos.y -(i * offsetY), OriginalPos.z);
OriginalPos.x = AllInvaders[k].transform.position.x;
k++;
}
}
Destroy(invader.gameObject);
}
}
here i also put the inspector of the two object. I hope someone can help me
Your answer
Follow this Question
Related Questions
problems with instantiating a moving prefab. 0 Answers
Having way too much difficulty synching my objects (Unity Networking) 0 Answers
IsTouching() not working right after Colliders2D movement/instanciation 0 Answers
How to make multiple objects spawn at random order and move ? 1 Answer
Problem instantiating objects one after another with mouse click 1 Answer