Moving walls in multiple places
For my game, I need to have 3 sets of wall on each players side of the screen that are each moving upwards and reappearing from the bottom of the screen once they reach the top and go off screen.
I have figured out how to do this with one of the sets of walls. However, my current method would require me to type the same thing many time over again in the script but with slightly different numbers for each of the different positions.
So what I'm wondering is there a simpler way for my to have my wall go off screen and come back up on the opposite Y-axis? Preferably a way that does not need me to type the exact position for each of the sets of walls.
Here is what I'm currently using in my wall script at the moment:
void CalculateMovement() {
transform.Translate(Vector3.up * _speed * Time.deltaTime);
if (transform.position.y > 6f)
{
transform.position = new Vector3(6.42f, -6, 5.996884f);
}
Answer by streeetwalker · Mar 02, 2020 at 07:22 PM
When you say 3 sets of walls, what do you mean by a "set"? Because now you have one wall move off the top and then reappear in another y location coming up from the bottom. Is that one set?
Do your wall "sets" - what ever those are - to appear in predetermined locations? If so, you can formulate an offset for each set, so that you only need to hard code the position for one set, and then add the offset to get the positions of the other sets.
Then instead of having a separate script for each set of walls, create a controller script that passes the move function and a reference current wall set to your move function, along with an index to which wall set is being moved.
It would probably be better to create an array of objects that holds a reference to each wall set.
So some pseudo code:
public GameObject wallPrefab; // drag your prefab to this inspector property
private GameObject[] wallsets;
private float yOffset = 10f; // for example
private int maxWallSets = 3;
private Vector3 startWallPos = new Vector3(6.42f, 6f, 5.996884f); //example
void Start() {
wallsets = new GameObject[ maxWallSets ];
for( i = 0 through maxWallsets ) {
Vector3 nextWallSetPos = startWallPos;
nextWallSetPos.y += i * yOffset;
wallSets[ i ] = intantiate your prefab here using nextWallSetPos
}
}
void Update() { // assuming you move everything in the update loop
for( i = 0 through maxWallsets ) {
CalculateMovement( wallSets[ i ] , i ) {
}
}
void CalculateMovement( GameObject g, int i ) {
g.transform.Translate(Vector3.up * _speed * Time.deltaTime);
if ( g.transform.position.y > 6f) {
Vector3 nextWallSetPos = startWallPos;
nextWallSetPos.y = -nextWallSetPos.y + i * yOffset;
g.transform.position = nextWallSetPos;
}
}
That's the gist of one approach. Probably needs some tweaking and fixing of course - that's a very quick look.
I'm sorry, I still new to unity so I'm having trouble understanding this.
Let me try to better explain my set up. Each player will have three walls on their side of the screen, each of the three walls are made of separate parts (about 6 or 8).
Basically the robots have to get to the other side and will break a piece of the wall when they collide. However, since the walls are constantly moving, the walls will have to be hit multiply times in order to get past and win.
@Bluesharky38, understood. Sorry, but your original question was, is there a simpler way to have walls appear at different locations without hand coding the locations for all walls. And I've given you an example
At the beginning you do need to hand code 1 reference location, with which you will base the location of all walls. You see that in my code above as startWallPos.
From there your code needs to deter$$anonymous$$e where the walls will appear based on that start position. You see that as yOffset. So each wall's y location is offset from start wall. (Now it occurs to me that maybe you want to offset the x and z locations of each wall ins$$anonymous$$d but the principle is the same).
So you need to come up with the offset values and you need to decide are these in regular intervals, or are they random, or what.
If they are regular, as you see in my code above, the offset is used with the loop index to place the location of the next wall - follow the math. If they are at random locations, then you need to set the starting location based on a random number generator, available in $$anonymous$$athf.Random.
Either way, you need to calculate the starting location of each wall as you instance them. Instancing means put a gameobject into your game based on a prefab (a kind of template). See 'Instantiate' in the Unity Scripting documentation.
You can see in the above code, also I keep a reference to each wall gameobject in an array when it is instanced. This is critical to the goal of "not having to hand code the location of each wall."
By putting reference to the new gameobject we instantiate, we can use that array to give us the individual wall when we want to move it, or destroy it, or what ever, as you see in the Update method.
What happens in Calculate$$anonymous$$ovement is just a mirror of the code you originally posted, but will handle all the walls in the array without you having to hand code anything but how the wall position changes when it hits your y value limit.
Now that is a very simple way of placing and moving walls without having to hand code the position of each and every wall.
You will need work to understand what my sample code is doing and adapt it your idea for you to move forward.
So, you've got some learning to do. I hope my explanation helps! Stick with it and you will prevail - it's not difficult to code games, but there is a lot to learn and it takes time!
Okay, just one more question. Do I need to put this in my player script, my wall script, or my game manager script?
Other than that, I think I should be able to figure things out based on what you've given me.
Thank you again for your time :)
Your answer
Follow this Question
Related Questions
Can someone help me rewriting my script from transform.position to transform.Translate? 1 Answer
How can I move an object between two positions, while it is on a rotating platform? 0 Answers
player tilting left or right on key press / while turning left or right? 0 Answers
Limit movement to just forward/backward and left/right 0 Answers