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 /
This question was closed Jan 26, 2015 at 03:25 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by Brokenarrow · Mar 31, 2014 at 11:55 AM · movementtransformpositionchildtranslate

Match position of two object without making one a child of the other

Hi,

I was wondering how I would go about this. Currently I tried this, but this just gives a boatload of errors.

playerbox.transform.Translate = new Vector3(transform.Translate.x, transform.Translate.y, transform.Translate.z);

This script is on object A, and playerbox is the object I want to match position and movement on (but not rotation!).

Any tips or hints in the right direction would be greatly appreciated!

Edit:

Thanks for the great advice! I tried some of it out, and I'm not sure if I'm doing it right, but

playerbox.transform.Translate(transform.position);

moves the playerbox in the correct direction, but at a different speed compared to the original object. I however intend to have the playerbox and the original object to stay at the same speed and location relative to each other, much like as if one was a child of the other.

playerbox.transform.position = transform.position;

Does what I want, but I do not want the playerbox object at the exact same location as the original object.

Any suggestions how I could do this?

Edit2:

I found a workaround which was better then my original design :) Thanks for the help guys, you really pointed me in the right direction!

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

4 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Gruffy · Mar 31, 2014 at 12:29 PM

HEY BUD. Okay here...

playerbox.transform.Translate = new Vector3(transform.Translate.x, transform.Translate.y, transform.Translate.z);

you are trying to match the Translate method a little wooozy like. So, if you wanted one transform object to = another you might try writing it like so...

 playerbox.transform.Translate(transform.position * time.deltaTime);

What you were trying to say with your code was a good attempt, but Unity needs it in the fashionabove.

All i did was take your code

new Vector3(transform.Translate.x, transform.Translate.y, transform.Translate.z);

and instead of assigning it with "=", it was assigned using the correct syntax for the method structure, which was...

Translate(transform.position);

with the parenthesis(curly brackets) being the important addition and only the need to reference your other transform`s position as the method requires, like you already sussed out there, a Vector3 coordinate.

The only real issue I would have with your code is that your are effectively making your playerBox tranform = the transform of the code that this script is attached to... For ease of use and understanding, perhaps make two public GameObject slots in your script and place both transforms you want to move in there and use there respective cache variable names as prefixes to your transform Translation object and the object you are translating to.. something like....

 public Transform playerBox; //accesible through inspector
 public Transform target;
 
 void Update()
 {
 playerbox.transform.Translate(target.transform.position);
 }
 
 
 
 

Anyway. hope that helps some bud. This should solve your issue, but let us know back here if not. Take care Gruffy

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
1

Answer by trololo · Mar 31, 2014 at 12:29 PM

Translate is a function, do the same with transform.position

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
1

Answer by Simon-Larsen · Mar 31, 2014 at 12:31 PM

You could directly set the position of your GameObject, like so:

 playerbox.transform.position = transform.position;
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 Simon-Larsen · Mar 31, 2014 at 12:32 PM 0
Share

Have to place a comment... Suddenly there were 3 other answers as I posted $$anonymous$$e, lol

avatar image Gruffy · Mar 31, 2014 at 12:38 PM 0
Share

lol, me too

avatar image
1

Answer by NickP_2 · Mar 31, 2014 at 12:31 PM

Just use:

 followingObject.transform.position = leadingObject.transform.position;

If you want to use a more smoother, or some kind of delay follow, you can use Vector.Lerp or Vector3.Slerp:

 public float smoothing = 4;
 
 followingObject.transform.position = Vector3.Lerp(followingObject.transform.position, leadingObject.transform.position, Time.deltaTime * smoothing);
Comment
Add comment · Show 1 · 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 BanditUnknown · Apr 05, 2020 at 06:41 AM 0
Share

This doesn't seem to work if you have fast moving objects. It works fine for rotations, but not for position, for some reason.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

How can I call this player transform.position code once, then stop? 2 Answers

Moving an Object - The right way. 2D 1 Answer

follow along the XZ plane? 0 Answers

how to stop child object from twitching? 1 Answer

transform.Translate but Transform has Rotation!=0 1 Answer


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