How to All instantiated objects to change position if any object hits defined border
Hello all,
Im new to Unity... but trying to do something "easy" and make a space invaders type game (to help me learn to code here).... pretty easy but am having issues having All instantiated objects drop/change direction when one hits the edge. I can do this no problem with one object... but what I want to do is once that lead object hits the edge... all of the other objects should also hit the else and drop and reverse direction.
Here is a code snipit from the objects movement script... this script is attached to the object. The all good is a bool that I was trying to use if one hit the edge to change it... but it isnt global.. maybe I need to do that? havnt worked with global variables yet though. (this code actually works but it is like a train... I dont want a train (one follows after the other... I want it to stop and move down the screen 3 spaces and reverse direction at that point....not continue to the edge like the lead one did.
void Update()
{
// checks to see if the invader is within the game boundries. If it is it will move it in the movedirection (either positive or negative)
// if not it will go to else
if (transform.position.x < gameEdgeBoundry && transform.position.x > -gameEdgeBoundry && allGood) {
transform.Translate(Vector3.right * invaderSpeed * Time.deltaTime * moveDirection);
}else
{
allGood = false;
moveDirection = moveDirection * -1.0f; //used to flip direction in expression above
//added the moveDirection * 1.0f) to move the invader one x spot in the correct direction based on movedirection being positive or negative
//this also move the invader down thre (for size of box and gap so they dont run into each other... only need one if I can get them to move together).
transform.position = new Vector3(transform.position.x + (moveDirection * 1.0f), transform.position.y, transform.position.z - 3.0f);
allGood = true;
}
}
Answer by TheGrobbler · Jan 25, 2021 at 12:04 AM
I also thought of making everything a child of one but that wont work because that parent might get killed so ? anyway... any help appreciated.
Answer by mattanmohel · Jan 25, 2021 at 02:04 AM
So if I'm understanding this correctly, you have a line of ships and you want them to all flip directions when they hit the wall. What I would do is give the script a static int multiplier. Static fields pretty much mean that the value is consistent between all instances of the script and changing it will change between all the instances. All that's left to do then is multiplying your speed by this static value whenever you move and negating the value whenever a ship has detected hitting the wall. (The value can be either 1 or -1, or true or false if you want to work with booleans)
kind of... say I have a line of ships... 1 2 3 4 5 moving -> all same speed. When 5 hits the end... I want all of them do now move down 3 and then move <- .... so currently what they do is they move -> then when 5 hits the wall it goes down and moves <- and then 4 hits the wall and does the same then 3, 2, 1... so like a train turning.... but what I want is 5 to hit and then when they are going <-- 1 should be the leader now and 2,3,4,5 should be behind... and 1-4 never hit the right wall.... if you see in lines 5-10 above that is what causes things to shift....
so I added the allGood bool so that when 5 hits the wall it changes to false...and I was hoping that 1-4 would jump to the else string...but they dont.
Sorry, I kind of skimmed over the code for my last response. Now that I looked at it though I think the best way to go about it is rather than have a parent ship, to have an independent script that contains the ship references as an array, then you could run the code of each ship in a loop and through that make sure that the ships all move at a certain time and order and maintain a connection and communication between them.