• 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
Question by alexander11 · Aug 09, 2016 at 12:58 AM · c#unity 5positionobject

How do i have multiple objects to rely on a objects position?

How do i have multiple objects to rely on a objects position, for an example, every of the objects rely on the main objects position kind of like whats in the picture below if i only move the red cube the rest of the other cubes follow the red cube, i know t$$anonymous$$s can be done if i set the other cubes as c$$anonymous$$ld's of the red cube but i don't want to it like that i just want it through script, would anyone know how to do t$$anonymous$$s?

alt text

capture09.png (78.9 kB)
Comment

People who like this

0 Show 0
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
Best Answer

Answer by wesleywh · Aug 09, 2016 at 01:16 AM

Adding it as a c$$anonymous$$ld would be easiest. The other t$$anonymous$$ng I can t$$anonymous$$nk of would be to offset your x,y,z coordinates by that of the red cube. Example you could have a public offset that is available to everyone. Make a script like the following on the red cude (Example script named "PositionScript"):

 public Vector3 offset = Vector3.zero;
 private Vector3 originalPos = Vector3.zero;
 
 void Start() 
 {
     originalPos = t$$anonymous$$s.transform.position;
 }
 
 void Update()
 {
     offset.x = t$$anonymous$$s.transform.position.x - originalPos.x;
     offset.y = t$$anonymous$$s.transform.position.y - originalPos.y;
     offset.z = t$$anonymous$$s.transform.position.z - originalPos.z;
 }

Then have somet$$anonymous$$ng like t$$anonymous$$s on the grey cubes:

 void Update()
 {
     t$$anonymous$$s.transform.position = t$$anonymous$$s.transform.position - GameObject.FindGameObjectByTag("redcube").GetComponent<PositionScript>().offset;
 }

Please note that none of these are actually tested to see what they do but the general idea is there. To get the same effect you can place it as a c$$anonymous$$ld (A lot easier):

 t$$anonymous$$s.transform.parent = GameObject.FindGameObjectByTag("redcube");

Then remove it when you don't want it to follow anymore:

 t$$anonymous$$s.transform.parent = null;

Comment
hoffmanuel

People who like this

1 Show 5 · 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 alexander11 · Aug 09, 2016 at 01:32 AM 0
Share

Thanks but is there way without using tags?

avatar image wesleywh · Aug 09, 2016 at 11:00 PM 0
Share

Ya sure. I just added tags because it is probably the next easiest thing to do. You could do it by the objects name.

 GameObject.Find("MyRedCube").GetComponent<PositionScript>().offset

I have to ask though, why not a tag or parent child relationship? You can have essentially unlimited tags. Name isn't the best way because you might forget what you named your object from one scene to another. I can't think of any other reliable way to get your object. The only other object finder is by object type. That won't work though because all the types here are going to be the same.

avatar image wesleywh · Aug 09, 2016 at 11:05 PM 0
Share

I just thought of another way actually. You could find the object with the PositionScript attached. (This goes along with my type idea):

 GameObject.FindObjectsOfType(typeof(PositionScript))

More on this at this answer:

http://answers.unity3d.com/questions/527447/find-gameobjects-with-a-certain-script.html

avatar image alexander11 wesleywh · Aug 09, 2016 at 11:57 PM 0
Share

Thanks again.

avatar image wesleywh · Aug 10, 2016 at 11:06 PM 0
Share

Nnnnoooo problem!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

221 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

Related Questions

Distribute terrain in zones 3 Answers

Move Object to location of Trigger? 1 Answer

moving random objects to random positions 1 Answer

How to segment a number line of game objects between two points dynamically? 1 Answer

Check if an object is changing position C# 3 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