Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TheGrobbler · Jan 24, 2021 at 11:54 PM · animationtranslateupdate functionvector3.positiondelta position

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;

             }
       
 }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · 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
0

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)

Comment
Add comment · Show 2 · 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 TheGrobbler · Jan 25, 2021 at 03:11 AM 0
Share

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.

avatar image mattanmohel TheGrobbler · Jan 25, 2021 at 03:23 AM 0
Share

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.

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

314 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

To change object Position Smoothly 1 Answer

Client RPC Question 0 Answers

Slow down animation with constantly pressing mouse,I have to keep pressing the mouse to slow the animation BlendSpeed. 0 Answers

my animation glitches when i add more keyframes 0 Answers

Does changing multiple sprites on a single animation affect performance? 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