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 /
  • Help Room /
avatar image
0
Question by Bluesharky38 · Mar 02, 2020 at 06:16 PM · movementscript.transform.positiontransform.translate

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


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

Comment
Add comment · Show 4 · 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 Bluesharky38 · Mar 09, 2020 at 04:47 PM 0
Share

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.

avatar image streeetwalker Bluesharky38 · Mar 10, 2020 at 02:06 PM 0
Share

@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!

avatar image Bluesharky38 streeetwalker · Mar 16, 2020 at 05:15 PM 0
Share

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 :)

Show more comments

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

279 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Translate isn't working correctly with NavAgent prefabs. I'm trying to initialize 36 enemies on 6x6 rooms. 0 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