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 /
avatar image
0
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 this can be done if i set the other cubes as child'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 this?

alt text

capture09.png (78.9 kB)
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 wesleywh · Aug 09, 2016 at 01:16 AM

Adding it as a child would be easiest. The other thing I can think 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 = this.transform.position;
 }
 
 void Update()
 {
     offset.x = this.transform.position.x - originalPos.x;
     offset.y = this.transform.position.y - originalPos.y;
     offset.z = this.transform.position.z - originalPos.z;
 }

Then have something like this on the grey cubes:

 void Update()
 {
     this.transform.position = this.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 child (A lot easier):

 this.transform.parent = GameObject.FindGameObjectByTag("redcube");

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

 this.transform.parent = null;

Comment
Add comment · 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("$$anonymous$$yRedCube").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))

$$anonymous$$ore 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.

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

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

Move Object to location of Trigger? 1 Answer

Saving Object Name in a database when it is clicked ? 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