Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by $$anonymous$$ · Mar 15, 2014 at 11:44 PM · movementtransform

Move multiple objects relative to another objects movement

I am trying to put together a little script that that moves multiple objects relative to another objects movements. These objects are not grouped to the "parent" object, and are not using the parenting feature of Unity. Below is an example of how I am currently attempting to accomplish this, but the Y axis keeps flipping from negative to positive on each update, which causes the objects to appear as if they are flashing.

 foreach (GameObject piece in PlayerPieces)
 {
     Vector3 offset = (this.transform.position - piece.transform.position);
     if (isAlive)
     {
         piece.transform.position = this.transform.position + offset;
     }
 }
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
1
Best Answer

Answer by tobyfee · Mar 16, 2014 at 12:51 AM

This doesn't look like the correct vector math. The problem, as I see it, is that 'offset' goes the entire distance from the piece to the target (I'm going to call 'this' the target for clarity's sake). Setting the piece's position to be the same as the target plus the offset means it will move to the target, then through the target the same distance and direction it needed to move to the target.

That can't be right, but I'm not totally certain of your goal: did you want the pieces to move toward the target constantly, like a heat-seeking missile, or do you want it to just move along with the target, like the way an umbrella moves with the person carrying it?

To get it the objects to maintain a fixed relationship to the target, parenting is the easiest way to go about it, but you mentioned you don't want to do that.

The other way to accomplish the same effect would be to capture the relationship between the two objects right at start and preserve it. e.g.

here be pseudocode

Public GameObject target; Private Vector3 offset;

  void Start(){
     offset = target.transform.position - this.transform.position;
     }
  void Update(){
     this.transform.position = target.transform.position + offset;
     }


Note that in this version the script is on the piece and not the central object, which will probably make more sense in the long run.

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 $$anonymous$$ · Mar 16, 2014 at 01:21 AM 0
Share

For what I am trying to do your analogy of the umbrella is perfect, I am trying to get a few objects to follow along with another object. In my case it is multiple objects, and I know that just applying a parent to the objects would be the easiest way, but it makes the rest of my code way to complicated.

What I have in my "controller" class is a generic object list, of the children of the controller, and each time there is movement on the controller the child positions are updated in relation to the controller's position.

 public List<GameObject> PlayerPieces = new List<GameObject>();
 void UpdatePlayerPieces()
         {
             foreach (GameObject piece in PlayerPieces)
             {
                 Vector3 offset = (this.transform.position - piece.transform.position);
                 if (isAlive)
                 {
                     piece.transform.position = piece.transform.position + offset;
                 }
             }
         }

I guess I could take the above code and have each game object have a link to the controller object, rather than the controller object linking to all of the child objects. That seems like it would be the best way, but it would require more rigging on my part which is fine. I will give that a shot, thank you for your help.

avatar image $$anonymous$$ · Mar 16, 2014 at 01:40 AM 0
Share

your recommendation worked perfect, thank you

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

21 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

Related Questions

Random Movement : 2d 1 Answer

Vector3 Returning Infinity Error 2 Answers

A node in a childnode? 1 Answer

Change int value for 30 sec... 1 Answer

object movement as wave in Unity 3d 2 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